ForeverDev

Swift example

Oct 15th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /*
  2. There is no Swift syntax highlighting on pastebin yet :(
  3.  
  4. Defining functions is pretty nasty. (variableName: valueType) (in this case, [Int] means an array containing integers)
  5.  
  6. You also have to define the return type after the arrow. [Int : Bool] means the function will return a dictionary with
  7. integer keys and bool values. For example: {1 : true, 5 : false}
  8. */
  9.  
  10. func arrayContains(search: [Int], match: [Int]) -> [Int : Bool] {
  11. var container = [Int : Bool]() //define a dictionary with integer keys and bool values, this dictionary will be returned
  12. for number in match { //iterate through the match array, set each number as a key in 'container' and set it to false
  13. container[number] = false
  14. }
  15. for element in search { //iterate through the array you're searching
  16. for compare in match { //also iterate through the comparison array so we can compare
  17. if compare == element { //if they match
  18. container[compare] = true //set the value of the key to true
  19. }
  20. }
  21. }
  22. return container
  23. }
  24.  
  25. let test = arrayContains([5, 9], [5, 9, 10])
  26.  
  27. println(test)
  28.  
  29. /*
  30. 5: true (found in the search array)
  31. 9: true (found in the search array)
  32. 10: false (not found in the search array)
  33. */
Advertisement
Add Comment
Please, Sign In to add comment