user3380292
user3380292

Reputation:

Splitting a string into an array

Pretty new to objective-C trying to understand how I can split a string I have. I actually think I split the string already, now I just want to test it out so I am trying to set it to a textview, getting an "incompatable pointer type" warning. I did read some things online, however they don't match with what I saw in class, and my professor told us iOS changes all the time, and that some solutions I might find might not work anymore. This is why I am coming to SO, I did try to find this information out on my own, however all SO links were 2+ years old so I wanted to be safe and re-ask this question for anyone using iOS 7 +.

- (IBAction)GetCourse:(id)sender
{
    __block NSString * jstring = nil;
    __block NSArray * jarray = nil;

    dispatch_queue_t progressQueue = dispatch_queue_create("JSON.GetJSONString", NULL);
    dispatch_async(progressQueue, ^{
            jstring = [JSONHelper JSONgetString:@"http://iam.colum.edu/portfolio/api/course?json=True"];
        dispatch_async(dispatch_get_main_queue(), ^{
            //main thread code
            //textView.text = jstring;
            jarray = [jstring componentsSeparatedByString:@" "];
            textView.text = jarray;
            NSError *error = nil;
            NSDictionary *results = [NSJSONSerialization JSONObjectWithData: [jstring dataUsingEncoding:NSUTF8StringEncoding]
                                                                    options: NSJSONReadingMutableContainers
                                                                      error: &error];
            if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
        });
    });

Upvotes: 1

Views: 109

Answers (1)

Aaron
Aaron

Reputation: 7145

Your problem is that textView.text is a property of type NSString and jarray is an NSArray. Try this instead to put the first course name string in the text view:

textView.text = [jarray objectAtIndex:0];

Then try something like this to put the whole batch of courses appended together into the text view:

NSString * fullString = [NSString string];
for(int i = 0; i < jarray.count; i++)
{
    fullString = [fullString stringByAppendingString:[jarray objectAtIndex:i]];
}
textView.text = fullString;

Update: After looking at the JSON response you probably do want to split the string you get as there is an easier way to get at the data. You want to serialize an NSArray of NSStrings from the JSON response. Your code is trying to serialize it to an NSDictionary which won't work because the response (as I see it) is an array of strings, not a dictionary of values. Try this at the bottom of your block:

NSError *error = nil;
NSArray * resultArray = [NSJSONSerialization JSONObjectWithData: [jstring dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers                                                                      error: &error];

if ( !resultArray ) {
    NSLog(@"Error parsing JSON: %@", e);
} else {
   for(NSString * course in resultArray) {
      NSLog(@"Course: %@", course);
   }
}

Upvotes: 2

Related Questions