-1

So I've been playing around with Swift 3 on playground and am just trying to convert some data analysis stuff I wrote in python (as an exercise in language difference, etc.) For some reason my for in loop isn't following through with appending the "userArr" arrays with the names from "users".

for (key, value) in friendships{
    userArr[key].append(users[value]!)
    userArr[value].append(users[key]!)
}

When I delete the .append(arg) I'm able to obtain correct references to each item I'm trying to find (and append), and the playground margin marks that the loop occurred 16 times. But for some reason the second I add the member function playground doesn't even say the for-in loop occurred once.

To be clear, the arrays I'm trying to populate are not being appended as they should be.

Is this a language specific issue with using member-functions in a for in loop? The Arrays and Dictionaries I'm referencing in the loop are printed below:

    let users = [

0 : "Jiro",
1 : "Dunn",
2 : "Lou",
3 : "Bri",
4 : "Thor",
5 : "Clive",
6 : "Rix",
7 : "Devin"

]

    let friendships = [ (0,1), (0,3), (1,7), (1,2),
                (2,5), (2,7), (3,2),
                (3,5), (4, 1), (4,6),
                (5,4), (5,1), (6,7),
                (6,2), (7,4), (7,0) ]

var Jiro: Array<String> = [], Dunn: Array<String> = [], Lou: Array<String> = [], Bri: Array<String> = [],
Thor: Array<String> = [], Clive: Array<String> = [], Rix: Array<String> = [], Devin: Array<String> = []

var userArr =  [Jiro, Dunn, Lou, Bri, Thor, Clive, Rix, Devin]
Ezra Goss
  • 124
  • 8
  • 1
    After executing the loop, the userArr is filled as one would expect. What result to you get and what do you expect? – Martin R Aug 21 '16 at 01:00
  • 1
    What do you _expect_ and what do you _actually_ get? – matt Aug 21 '16 at 01:01
  • And please show your actual code, not in pieces in reversed order and separated by discussion, but the actual code. Thanks. – matt Aug 21 '16 at 01:09
  • @matt I have been told here before to show a precise problem and then any ancillary code on the bottom so as to not convolute. But sorry for the confusion – Ezra Goss Aug 21 '16 at 01:11
  • @MartinR my userArr items are not filled after executing the loop. And I expect them to be – Ezra Goss Aug 21 '16 at 01:11
  • I have copied your code into an Xcode project (with the loop at the end) and executed it. `print(userArr)` after the loop showed `[["Dunn", "Bri", "Devin"], ["Jiro", "Devin", "Lou", "Thor", "Clive"], ["Dunn", "Clive", "Devin", "Bri", "Rix"], ["Jiro", "Lou", "Clive"], ["Dunn", "Rix", "Clive", "Devin"], ["Lou", "Bri", "Thor", "Dunn"], ["Thor", "Devin", "Lou"], ["Dunn", "Lou", "Rix", "Thor", "Jiro"]]`. – Martin R Aug 21 '16 at 01:15
  • @EzraGoss Your goal in providing code should be for me (or whoever) to copy it, paste it, and run it. – matt Aug 21 '16 at 01:15
  • @MartinR thank you that tells me that it's something happening with my playground. Sorry for the inconvenience – Ezra Goss Aug 21 '16 at 01:16
  • @matt understood, I'll make sure to do that next time – Ezra Goss Aug 21 '16 at 01:16
  • @EzraGoss Playgrounds are the work of the devil. They don't work the way Swift really works. Run your code in an actual app so the playground doesn't get in your way, mislead you, etc. – matt Aug 21 '16 at 01:18
  • @matt Thank you that's good to know moving forward. Actually reading your book now so thank you twice for good advice! – Ezra Goss Aug 21 '16 at 01:21
  • 1
    I am rewriting it at this very moment! :)))) – matt Aug 21 '16 at 02:13
  • @matt Oh no! My version is out-to-date... ;) – FredericP Aug 21 '16 at 08:00

1 Answers1

0

Given that other users were able to run this code successfully, I'll close this as an issue with playground

Ezra Goss
  • 124
  • 8