Git Product home page Git Product logo

link_swift-toolbox's Introduction

swift-toolbox

Swift Cookbok: Arrays

var anArray = ["one", "two", "three","one"] // Mutable let anImmutableArray = ["four", "five"] // Immutable

first element of array

anArray.first // a Swift solution. nil if empty anArray[0] // Classic solution. Error if empty

last element of array

anArray.last // a Swift solution. nil if empty anArray[anArray.count-1] // Classic solution. Error if empty

Empty Array

var emptyArray:[String] = [] if emptyArray.isEmpty { print("Empty Array") } emptyArray.first // Returns nil emptyArray.last // Returns nil

Does value exist in array?

if anArray.contains("three") { print("Array contains value") }

How to filter array to get unique items?

// Create a Set then build array from it. Set guarantees uniqueness var aSet:Set = [] anArray.forEach{aSet.insert($0)} // Could do this: aSet = Set(anArray) aSet.count anArray = Array(aSet) anArray.count

How to merge arrays?

// Just like prior solution. Uses Set's union function // Also skip closure anArray = ["one", "two", "three","one"] aSet = Set(anArray) aSet.unionInPlace(anImmutableArray) aSet.count anArray = Array(aSet) anArray.count

Sort Array

anArray = ["one", "two", "three","one"] let sortedArray = anArray.sort() // Returns a new sorted array sortedArray anArray anArray.sortInPlace() // Update the array in place anArray

Find the index of an element in an Array

anArray = ["one", "two", "three","one"] anArray.indexOf("two") // indexOf() can also be used to search an array let aNumericArray = [1, 2, 10, 20, 100] let x = aNumericArray.indexOf({$0 > 10}) // Find the index of first value greater than 10 x

link_swift-toolbox's People

Contributors

r14r avatar

Watchers

James Cloos avatar Ralph Göstenmeier avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.