last day (14 days later) » 

1:48 AM
0
A: fetching integers using valueForKey using swift

vacawamachange: var mydata = NSArray() to: var mydata:[Int] = [] and replace: self.mydata = self.jsonData.valueForKey("id") as NSArray! with: for dict in self.jsonData as [NSDictionary] { if let idnum = dict["id"] as? Int { println("id is an Int") self.mydata.append(idnum) ...

 
sorry, can you clarify where I should add this to my code...:/
Also i get, Type 'Int' does not conform to protocol 'StringLiteralConvertible'
 
It looks like you have an array with a dictionary as its only element. Try my edit.
 
i receive the error, NSNumber is not a subtype of NSArray
 
what does println(self.jsonData) print?
 
I've updated the code above
 
1:48 AM
Do you want self.mydata to be the array of id values: [1,2,3,4,5,6]?
 
Yea thats what i've been trying to do....:/
 
I'm not convinced id is an Int, so I coded both.
 
error: NSArray does not have a member name 'append'
 
Change mydata to var mydata:[Int] = []
 
it prints id is an Int. multiple times
 
1:49 AM
is my data now populated with the idnums?
 
let me check
yea it is
actually
thanks....so essentially i cannot store integers in an NSArray?
was that the issue
 
Well, you can store them as NSNumbers, but Int is not an object type. What you needed to do was to loop over the array of dictionaries getting each value and appending it to the array.
Will you want to do the same for the names? Do you want a mynames array to hold the names?
 
i was able to do that with valueForKey just fine. apparently the method documentation stated that it only does strings
which is why i was having a hard time figuring it out
with integeres
integers*
 
Ah, OK.
 
thanks for all your help. really appreciate it
 
1:56 AM
Glad I could help.
 
2:40 AM
I played with this more. Could you try: self.mydata = self.jsonData.valueForKey("id") as [Int] instead of the for loop?
 

last day (14 days later) »