1

Below I have a constant dict that is of NSMutableArray type that stores my data extracted from JSON. I've tested that I'm able to extract what I need when referring to variable name.

While I know that appending a normal array would be myArrayValue.append("String"), however I tried the same method with my 2D array it doesn't work.

I'm quite new to Swift2 programming, please do explain in detail. Thanks in advance!

let dict: NSMutableArray!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray

var arrayObject : [AnyObject]
var arrayName : [[String]] = [[]]
var name : String

for var x=0; x<dict.count; x++ {
        arrayObject = dict[x].allValues
    for var y=0; y<5; y++ {
        name = arrayObject[y].description as String
        arrayName[x][y].append(name)
    }
}

2 Answers 2

2

I won't fix your code myself, there's too much work, I'm just telling you what is needed:

  • You don't want to use Foundation's objects anymore if you don't actually need them, use Swift arrays.

  • Don't call "dict" something that is an array.

  • Don't force unwrap your values... use safe unwrapping with techniques like if let.

  • Don't force with try!, use Do-Catch and Try instead, and handle errors.

  • Don't use MutableContainers if you don't need this feature. Use an empty array instead for passing no options.

  • Don't wrap in parenthesis things that don't need to be wrapped in parenthesis.

  • Don't use manually indexed loops when you can use Swift paradigms like for item in array { print(item) }.

  • arrayName[x][y] can't work if arrayName[x] is nil.

Once you've done these fixes and improvements, your current problem will disappear because your code will be easier to follow and maintain.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! I'm very new to Swift, and will try my best to interpret what you meant.
You're welcome. That's a very good idea to try and improve your knowledge by yourself, best way to learn! On the other hand, don't hesitate to ask new questions if you're stuck somewhere - we'll be glad to help.
0

Your post is hard reading, you didn't explain well your problem and I suppose it should be downvoted. Update your issue, if my answer is not correct, hope, it helps:

let JSONDict: [[String: AnyObject]] = [["name" : "value1"], ["name" : "value2"]];
var arrayName = [String]()
for dict in JSONDict {
    if let value = dict["name"] as? String {
       arrayName.append(value)
    }
}

1 Comment

Sorry that my post is hard to read. Basically, I just need to store Strings into my 2 dimensional array. arrayName[0][0] = "String Value" fails to execute. Side track, because I have a data of 3000+ rows by 7 columns, with increasing rows over time due to more data. Would 2 dimensional array be the way to manage my data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.