Convert NSString to few NSIntegers

77 views Asked by At

I have getting data from plist to NSString, in result I see something like this "{{1848,594},{154,176}}". What is the best way to convert every single number to separate NSInteger?

NSString *frame = [myPlistKey objectForKey:@"frame"];

How to convert frame to 4 separate integers?

2

There are 2 answers

0
mipadi On BEST ANSWER

In this specific case, it looks like you are trying to parse the string representation of an NSRect, in which case you can just use NSRectFromString() from the Foundation framework.

1
Jakub Truhlář On

Edit: Since you are not much specific, I will try to cover your situation. If i count with the fact you have NSString *frame filled with {{1848,594},{154,176}}:

    NSString *stringWithoutLeftBracket = [frame
                                     stringByReplacingOccurrencesOfString:@"{" withString:@""];
    NSString *stringWithoutRightBracket = [stringWithoutLeftBracket
                                     stringByReplacingOccurrencesOfString:@"}" withString:@""];
    NSArray *frameArray = [stringWithoutRightBracket componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

Then you can access objects with indexes like:

NSInteger integer = [[frameArray objectAtIndex:0] integerValue];

But also you could use a for loop like this:

for (NSInteger integer in frameArray) {

    // Do something
}

In my opinion you have a string made from rect, means you could convert it.