简体   繁体   中英

How to convert an NSString to an NSArray?

I have an NSString , that when I NSLog , it looks like this:

(
    Music,
    Music,
    "Ao Dai Fashion Show"
)

I would like to convert this to an NSArray, where array[0] = Music, array[1] = Music, and array[2] = Ao Dai Fashion Show.

I have tried to first remove the () using:

NSString *jsonString = [str stringByReplacingOccurrencesOfString:@"()" withString:@""];

However, I receive an unrecognized selector sent to instance .

How can I achieve this?

Thanks

This is the original array:

NSArray *oriArray = [dictionary objectForKey:someKey];

NSLog :

(
        {
        "end_time" = "21:00";
        "english_event" = Music;
        "english_performer" = "DJ Happee From Channel 93.3";
        "start_time" = "20:00";
    },
        {
        "end_time" = "21:00";
        "english_event" = Music;
        "english_performer" = "Adam Cease";
        "start_time" = "20:00";
    },
        {
        "end_time" = "21:00";
        "english_event" = "Ao Dai Fashion Show";
        "english_performer" = "";
        "start_time" = "20:00";
    }
)

Here is the code to get the new NSArray containing contents Music, Music, "Ao Dai Fashion" :

NSArray *newArry = [oriArray valueForKey:@"english_event"];

However, the newArry is giving me an error when I try to access its index.

I don't think that what you're logging is actually an NSString . Please check its type because it seems like it's already an instance of NSArray . Therefore you should already be able to do what you want.

-(void)parse:(NSDictionary*)dictionary{

NSArray *oriArray = [dictionary objectForKey:someKey];
NSMutableArray *arrray=[[NSMutableArray alloc] init];
for (NSDictionary *dic in oriArray)
{
    [arrray addObject:[dic objectForKey:@"english_event"]];

}

}

use this it may help you....

Sorry I din't have the reputation to comment, I just thought of asking this, did you try- NSArray *myArray = [stringOi componentsSeparatedByString:@","]; (*where stringOi is your str) I tried with the following sample and this works, please do try yours.

NSString *stringOi=[NSString stringWithFormat:@"( Music, Music, "Ao Dai Fashion Show" )"]; NSLog(@"%@",stringOi);

NSString *jsonString = [stringOi stringByReplacingOccurrencesOfString:@")" withString:@""];

NSString *jsonStringFinal = [jsonString stringByReplacingOccurrencesOfString:@"(" withString:@""];

NSLog(@"%@", jsonStringFinal);

NSArray *myArray = [jsonStringFinal componentsSeparatedByString:@","];
NSArray *myArray1 = [myArray objectAtIndex:0];
NSLog(@"%@",myArray1);
NSArray *myArray2 = [myArray objectAtIndex:1];
NSLog(@"%@",myArray2);
NSArray *myArray3 = [myArray objectAtIndex:2];
NSLog(@"%@",myArray3);

Alternatively instead of oriArray you can get the values in a dictionary dict and then,

NSArray *myArray1 = [dict objectForKey:@"english_event"]; NSString *Music= [myArray1 objectAtIndex:0];

Hope for the best..!

Two things:

  1. NSArray *myArray = [myDictionary objectForKey:@"Valid Key"] does not mean that myArray is actually an Array. It just means it's a pointer to an object that you've cast as an NSArray .

  2. You can convert any NSArray or NSDictionary to a string using [myDictionaryOrArray description] .

If you don't care about taking extra steps and know that when you log out the array you get what you want/are expecting, then you can definitely get your string into an array. For example:

NSString *myString = [@[Music, Music, "Ao Dai Fashion Show"] description];
NSMutableArray *stringAsArray =  [NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@","]];
// this loop removes the white space before and after each word in the array
for (int i = 0; i < stringAsArray.count; i++) {
    stringAsArray[i] = [stringAsArray[i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

If you need more specific code for the longer sample you gave, I'll be happy to update my answer.

(
    Music,
    Music,
    "Ao Dai Fashion Show"
)

this is an array not a string .

This array holds three strings in each indexes , Music, Music, Ao Dai Fashion Show

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM