0

Yes, I know the title seems a bit to obvious, and can easily be searched for the solution. I know how to convert a 1-D NSArray to an Array, that is simple enough.

However, that is not my issue, as my NSArray is different. Please see the following code

NSArray *artists;

artists = @[@"Performed by: Legendary Group", @"Performed by: Ivan Cheong",
          @"Performed by: Tien Nguyen", @"", @[@"Performed by: DJ Happee From 
            Channel 3.3", @"Performed by: Adam Cease", @""], 
            @"Performed by: Music Between California and Summer", @""];

Then in cellForRowAtIndexPath, I was about to access it like so:

- (UITableViewCell *)tableView:(UITableView *)tableView 
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    if ( [ [artists[indexPath.section] objectAtIndex:indexPath.row] 
                isEqualToString:@""] )
    {
        ....
    }
}

As you can clearly see, my NSArray contains another array at index 4. I require this for trivial purposes.

I would like to know is it possible to convert my NSArray to a Swift standard Array type?

I have tried to implement the equivalent:

var temp = NSArray()

let artists = ["Performed by: Legendary Group", "Performed by: Ivan Cheong",   
           "Performed by: Tien Nguyen", "", ["Performed by: DJ Happee From 
        Channel 3.3", "Performed by: Adam Cease", ""], "Performed by: Music 
             Between California and Summer", ""]

temp = artists

This works properly as I want, however, this is not up to Swift 2.0's coding standards.

I want to use an Array, but I don't know if this is possible?

Thanks

0

1 Answer 1

1

Create an array of AnyObjects:

let artists: [AnyObject] = ["Performed by: Legendary Group", "Performed by: Ivan Cheong",
    "Performed by: Tien Nguyen", "", ["Performed by: DJ Happee From Channel 3.3", "Performed by: Adam Cease", ""], "Performed by: Music Between California and Summer", ""]

Then check the type of each item in your tableView function and handle accordingly:

 func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {

        switch artists.indexPath {
        case is String: //handle string
        case is NSArray: //handle array
        default: //trap other stuff


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

6 Comments

Please see how I initialized the NSArray artists carefully. I have defined an array within an array, and I am having trouble translating "that" array to Swift. It would be easy for me to translate it to Swift if it was a 1-D array, as you have shown, but it's not 1-D. I have updated my question for clarity.
I'm not clear on why you have an array in an array here. Can you explain?
I use it to display a schedule of events, however, that is irrelevant to my issue.
Edited to address your issue. Hope this helps.
There's no way to directly achieve this with Array type conversion?
|

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.