I am trying to load data from a json response stored in an NSArray into a tableview.
json:
"fulltime": [
2,
2
],
Above is the json but when I display to screen it looks like the below code.
( 2, 2)
I have tried using the following to remove unwanted characters the brackets () in this case but i am getting the warning below the code.
NSArray *place= [jsonResults objectAtIndex:indexPath.row];
NSString *score= [place valueForKey:#"fulltime"];
Firstly tried this:
NSString *score = [[[place valueForKey:#"fulltime"]objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:#"(" withString:#""];
And then this:
NSString *jsonstring = [score stringByReplacingOccurrencesOfString:#"\)\n" withString:#""];
jsonstring = [jsonstring stringByReplacingOccurrencesOfString:#"\t" withString:#""];
This is the error i get each time:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1f55f270'
I am not sure is it a problem with the way i am trying o remove the characters or the way i am parsing the data. Below is a better view of the json, everything else parses fine up until i want to access "fulltime"
"date": "2013-03-17 16:00:00",
"home_id": 8455,
"home": "Chelsea",
"homeshort": "Chelsea",
"away_id": 8654,
"away": "West Ham United",
"awayshort": "West Ham",
"status": "Finished",
"halftime": [1, 0],
"fulltime": [2, 0],
"extratime": [0, 0],
"penalties": [0, 0],
try like this may be it'l helps you,
[[[jsonarray objectAtIndex:0] valueForKey:#"fulltime"] objectAtIndex:0]
Not sure if I understood what you want, but to parse those numbers I would do something like:
NSDictionary * place= [jsonResults objectAtIndex:indexPath.row];
NSArray * fulltime = [place valueForKey:#"fulltime"];
NSNumber * num1 = [fulltime objectAtIndex:0];
NSNumber * num2 = [fulltime objectAtIndex:1];
NSLog(#"Fulltime is %d, %d", [num1 intValue], [num2 intValue]);
Related
I'm trying to split a string and separate it into variable but am getting the following error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 ..
1]'
Code:
NSLog(#"%#", strResult);
NSArray* LocInfo = [strResult componentsSeparatedByString: #"|"];
NSString* Response1 = [LocInfo objectAtIndex: 0];
NSString* Response2 = [LocInfo objectAtIndex: 1];
NSString* Response3 = [LocInfo objectAtIndex: 2];
Any ideas? Thanks!
Your strResult is broken into LocInfo array that contains only two elements, and you tried to access third one.
As your string already contains 1 / 2 or 3 NSStrings, therefore no need to again store then into NSString, you can directly use them by LocInfo[index].
If you need to check how many strings are there simply use : [LocInfo count];
I need to extract some info from a number of strings in infoArray. The layout for each element in infoArray looks like #"NO,YES,YES,YES,YES,NO,NO,YES", etc.
When I use the following:
[splitArray setArray:[[infoArray objectAtIndex: i] componentsSeparatedByString:#","]];
I can access whatever I need in the new splitArray with something like:
NSLog(#"Result = %#", [infoArray objectAtIndex: 0]);
and in this case it will print out correctly. The problem is when I need to use it next for conditions:
if([infoArray objectAtIndex: 0] == #"YES"){
//do something
}
The above code obviously doesn't work even if the value was "YES". I believe the typecast is wrong. I'm a bit of a newbie. Any suggestions?
You should modify your question with the additional data instead of answering your own question.
To answer your question, use:
[[splitArray objectAtIndex: 0] isEqualToString:#"YES"]
Sorry about that I made an error in my notes:
NSLog(#"Result = %#", [infoArray objectAtIndex: 0]);
should be
NSLog(#"Result = %#", [splitArray objectAtIndex: 0]);
and
if([infoArray objectAtIndex: 0] == #"YES"){
should be
if([splitArray objectAtIndex: 0] == #"YES"){
I am trying to generate predicate editor templates for my Core Data entities. In my code I have the following:
NSEntityDescription *descrip = [NSEntityDescription entityForName:#"Person" inManagedObjectContext:managedObjectContext];
NSArray *templates = [NSPredicateEditorRowTemplate templatesWithAttributeKeyPaths:[NSArray arrayWithObjects:#"name", #"age", nil] inEntityDescription:descrip];
[ibPredicateEditor setRowTemplates: templates];
NSPredicate *p = [NSPredicate predicateWithFormat:#"name like 'John'"];
[ibPredicateEditor setObjectValue:p];
Printing out the contents of the templates array gives me the following:
CFArray 0x1002d7400 [0x7fff70ff5f20]
{type = immutable, count = 2, values =
(
0 : NSPredicateEditorRowTemplate
0x10025c090: [name] [99, 4, 5, 8, 9]
NSStringAttributeType
1 : NSPredicateEditorRowTemplate
0x1002d2dc0: [age] [4, 5, 0, 2, 1, 3]
NSInteger16AttributeType
)}
When this code executes I get the following on the console:
Warning - unable to find template matching predicate name LIKE "John"
The interface for doing this looks extremely straight forward, so I can't seem to figure out what I am doing wrong. Any help would be greatly appreciated!
EDIT
My original issue was that I was using the LIKE operator when my templates did not support it. However I am confused about why I get a similar warning when passing in a compound predicate into the editor.
NSPredicate *p = [NSPredicate predicateWithFormat:#"name CONTAINS 'John'"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:#"name CONTAINS 'Jo'"];
NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:p, p2, nil]];
[ibPredicateEditor setObjectValue: final];
OR
NSPredicate *final = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:p, p2, nil]];
[ibPredicateEditor setObjectValue: final];
Both of these produce similar warnings as my initial issue. However, I find it odd that I can use a single predicate and build a compound and predicate, but I can't pass a prebuilt compound and predicate into the editor.
The default templates supplied by NSPredicateEditorRowTemplate for NSString key-paths do not support comparisons with the LIKE operator.
In the templates you generated there is a list of the operators it does support:
NSPredicateEditorRowTemplate 0x10025c090: [name] [99, 4, 5, 8, 9] NSStringAttributeType
[99, 4, 5, 8, 9] indicates that the name NSString attribute supports:
4 – Equal To
5 – Not Equal To
8 – Begins With
9 – Ends With
99 – Contains
(found in NSComparisonPredicate.h)
CONTAINS is similar to LIKE without % and _ substitutions. If you require that you can always initialize your own template array.
The syntax is a bit verbose.
NSExpression *nameExpression = [NSExpression expressionForKeyPath:#"name"];
NSArray *operators = [NSArray arrayWithObjects:
[NSNumber numberWithInt: NSEqualToPredicateOperatorType],
[NSNumber numberWithInt:NSNotEqualToPredicateOperatorType],
[NSNumber numberWithInt:NSLikePredicateOperatorType],
[NSNumber numberWithInt:NSBeginsWithPredicateOperatorType],
[NSNumber numberWithInt:NSEndsWithPredicateOperatorType],
[NSNumber numberWithInt:NSContainsPredicateOperatorType],
nil];
NSUInteger options = (NSCaseInsensitivePredicateOption |
NSDiacriticInsensitivePredicateOption);
NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc]
initWithLeftExpressions:[NSArray arrayWithObject:nameExpression]
rightExpressionAttributeType:NSStringAttributeType
modifier:NSDirectPredicateModifier
operators:operators
options:options];
It may be a problem with the LIKE operator. This code logs no warning:
NSPredicate *p;
p = [NSPredicate predicateWithFormat:#"name == 'John'"];
[ibPredicateEditor setObjectValue:p];
p = [NSPredicate predicateWithFormat:#"age > 18"];
[ibPredicateEditor setObjectValue:p];
I am wondering how to get a NSFileManager listing to display the results in the order that the Finder sorts them. By default, this code:
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *foundErrors = nil;
NSArray *contentsOfDockDirectory = [fileManager contentsOfDirectoryAtPath:#"/Users/me/Desktop error:&foundErrors];
Generates a NSArray that lists it in this type of order: 1, 100, 2, 200, etc. However, the Finder shows it in it's correct sorting so it's: 1, 2, 100, 200, etc.
Is there some way to sort either NSArray or NSFileManager in order to have the listing in this order?
There is a section in the String Programming Guide called "Sorting strings like Finder", which is exactly what you are looking for.
I want to pass parameters in an NSURL. For example, I have
NSURL *url = [NSURL URLWithString:#"http://demo.digi-corp.com:82/Nilesh/betBuddy/api/getEventsXML.php?sp_ID=2"];
where sp_ID can be 1, 2, 3, 4, etc.
How can i do it? Please help me out.
You can use the method [NSString stringWithFormat:#""] to format your URL string. For example,
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://e.com/?var=%d", 2]];
If you look in the documentation for string format specifiers you can find out exactly what characters to use for each type of replacement (integer, double, object, etc).