Returned JSON data is changing from NSDictionary to NSArray - objective-c

I'm having a little difficulty with a JSON service that I'm consuming and iterating over. When I consume the service I am looping over the data as you would expect because of the number of records.
I'm saving that loop'ed data into an NSArray which I use later in a UITableView. Next I'm simply allowing the user to tap the selected row (from the json data result) to show more detail. Pretty simple so far.
Every element from the JSON service is NSString. So far nothing tricky. However, one element within the NSArray after the service has been put into the NSObject is showing HEX code, see below.
altitude NSString * 0x7ff8d4cd3d30 0x00007ff8d4cd3d30
Of course the app has a meltdown because it can't figure out what HEX is when I'm using that NSArray object to display key elements i.e. altitude. Now the odd thing is every other element within the NSArray looks like this see below.
latitude __NSCFString * #"21.45852" 0x00007ff8d4ca54f0
I have read a few suggestions stating this is normal for NSString and JSON data. But not really how to fix it.
What I have found is that NSArray after the JSON is complete is changing just that one element. I have also tried changing it from an INT to an NSString however same result (I know its a NSString in the first place btw, I was just trying different ideas.)
Abstract of JSON Call and loop to add into NSArray object.
//Do something with returned array
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *pilotJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//Loop through the JSON array
NSArray *currentPilotsArray = [pilotJson valueForKeyPath:#""];
//set up array and json call
pilotsArray = [[NSMutableArray alloc]init];
NSArray *keys=[pilotJson allKeys];
for (NSString *key in keys){
NSDictionary *elementDictionary=pilotJson[key];
NSString *altitude = elementDictionary[#"altitude"];
NSInteger n = [altitude intValue];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *string = [formatter stringFromNumber:#(n)];
NSString *nAltitude = [NSString stringWithFormat:#"%# ft", string];
[pilotsArray addObject:[[LiveMap alloc]initWithaltitude:nAltitude ]];
.
.
.
So when I get to this point of the code where the user taps the relevant record I get a crash and the application aborts. I'm assuming this is from the above NSString vs __NSCFString
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Pass the details the the detail view controller
PilotsFlightDetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"FlightDetails"];
NSLog(#"array: %#", pilotsArray);
NSString *AltitudeString = [[self.pilotsArray valueForKey:#"altitude"]objectAtIndex:indexPath.row]; <-----WIGS OUT HERE
I find this super odd as every other element works normally, but this one simply has issues. Any suggestions?
UPDATE:
NSLog of pilotsArray as per the request.
[1] LiveMap * 0x7f8b0265e1b0 0x00007f8b0265e1b0
altitude __NSCFString * #"21 ft" 0x00007f8b02664860
Also the jsonArray from the Service Directly.

Okay. I got it working. There was nothing wrong with the code. It however recognised another NSObject that had a same name "altitude" and for some reason it was getting mixed up.
I changed the name in the NSObject to something entirely unique and updated the instances in the relevant places. This did it. Lesson learnt always make sure you have named your variables appropriately.

Related

how to detect pasteboard item type

I am trying to identify between three types of objects:
if it is a URL of a file
If it is a URL of a directory
if it is a simple string
up till now, I have just this code, which does not work!
NSArray * classes = nil;
classes = [[NSArray alloc] initWithObjects:[NSURL class],
[NSAttributedString class],[NSString class], nil];
NSDictionary *options = [NSDictionary dictionary];
NSArray * copiedItems = nil;
copiedItems = [pb readObjectsForClasses:classes options:options];
Now I try to take the first object of the array copiedItems and try to call "types" property and i get a crash!
Check here and here:
You would need to use these pasteboard types, instead of the ones you're using.
NSString *NSStringPboardType;
NSString *NSFilenamesPboardType;
NSString *NSPostScriptPboardType;
NSString *NSTIFFPboardType;
NSString *NSRTFPboardType;
NSString *NSTabularTextPboardType;
NSString *NSFontPboardType;
NSString *NSRulerPboardType;
NSString *NSFileContentsPboardType;
NSString *NSColorPboardType;
NSString *NSRTFDPboardType;
NSString *NSHTMLPboardType;
NSString *NSPICTPboardType;
NSString *NSURLPboardType;
NSString *NSPDFPboardType;
NSString *NSVCardPboardType;
NSString *NSFilesPromisePboardType;
NSString *NSMultipleTextSelectionPboardType;
There's an pasteboard type for URLs. To distinguish between a file and a folder, you would need to instantiate an NSURL object with the pasteboard data, and find out if it is a directory by querying its attributes.
EDIT:
You also need to consider if the pasteboard data is being put there by your own application or other applications. If it's being put by other applications, I'm not sure the pasteboard types with the classes will work.
I use something like this in one of my projects:
supportedTypes = // array with supported types, maybe from the list
NSString *type = [pasteboard availableTypeFromArray:supportedTypes];
NSData *data = [pasteboard dataForType:type];
types is a method on NSPasteboard used to tell you what is available from the pasteboard. So, you shouldn't call it on the items you get back from the pasteboard.
If you're going to request multiple class types, iterate over the response and check the class type of each item, then decide how to interact with it.
Alternatively, decide which class type of data is most useful and make individual class type requests to the pasteboard. If you get a result back, use it and carry on, if not, try the next most useful class type. Look at using canReadObjectForClasses:options: to make this easier.

Trying to add entries in NSArray to NSDictionary, but values in NSDictionary are getting deallocated

I have three Deck Objects which are mostly just wrappers around NSArray objects containing Card objects. Initially, all the Card objects are in deck1, and eventually move through deck2 and deck3.
I also have an NSDictionary object that maps NSString objects to Card objects. I use the cards in deck1 to build this lookup table at the beginning of the game like this...
-(NSDictionary *)buildLookupTable {
NSMutableDictionary *lookup = [[NSMutableDictionary alloc] init];
for (Card *card in self.cards) {
NSString *lookupCode = [self buildCode:card];
[lookup setObject:card forKey:lookupCode];
}
return lookup;
}
I then pass the NSDictionary and the Decks to a layer object, but when I attempt to lookup a given Card based on a NSString, I get "-[CFString hash]: message sent to deallocated instance", but I can easily find the Card I'm looking for, not deallocated, in deck1 or deck2.
The NSString I'm using as a key to retrieve the desired value isn't deallocated, nor is the NSDictionary itself. I have even iterated over the return from the NSDictionary object's allValues method, and none of those are deallocated either.
What other deallocated objects could there be?
Edit-version2:
I've narrowed it down a bit.
In the lookup code this works
NSString *key = #"4-0"; //(NSString *)sprite.userData;
return [self.cardLookup objectForKey:key];
but this doesn't
NSString *key = (NSString *)sprite.userData; // value is #"4-0"
return [self.cardLookup objectForKey:key];
In the debugger sprite.userData looks fine.
sprite.userData is defined in the Card class buildSprite method as...
sprite.userData = [NSString stringWithFormat:#"%i-%i",self.x, self.y];
It looks like calling copyWithZone on key doesn't work (like it would for any other NSString).
Found the issue!
The problem was with the sprite.userData object and how it was created.
sprite.userData = [NSString stringWithFormat:#"%i-%i",self.x, self.y];
Evidently sprite.userData was getting released (thought the Xcode debugger seemed to know what it was, possibly because I had zombie objects enabled). The correct version is...
sprite.userData = [[NSString stringWithFormat:#"%i-%i",self.x, self.y] retain];

Objective C - EXC_BAD_ACCESS on [NSArray count] but can't see how

I'm working on a piece of code that GETs a URL and parses the contents for data between a asset of tags. In this case, it's looking for the code between and . When the URL body returned doesn't contain those tags, the cullXML: method returns an empty array like this:
return [NSArray arrayWithObjects: nil];
I check to be sure that the returned array has objects with:
if ( matchesXML.count ) {
In my debug code, I check twice. The first time, it works fine. The second time, just a couple lines later, it crashes. I can't see why. The lines look the same to me.
What am I missing?
A bigger chunk of the code is included below. Thanks!
if (self.newResults) {
NSString *urlResult;
NSArray *matchesXML;
NSArray *match;
NSDictionary *currentResult;
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
NSArray *returnedObjects;
NSArray *dictionaryKeys;
NSMutableArray *currentResultObjects;
int i = 0;
// determine tournament type, because the fields are different per type
NSString *tournamentType;
tournamentType = [[AFMethods tournamentTypeFromId:self.inputTournamentId] objectAtIndex:0];
urlResult = [NSString stringWithFormat:#"%#", responseHandle];
[responseHandle release];
NSLog(#"urlResult retrieved: %#", urlResult);
matchesXML = [AFMethods cullXML: urlResult forTag: #"matches"];
NSLog(#"matches loaded: %#", matchesXML);
NSLog(#"matchesXML.count %i", matchesXML.count);
if ( matchesXML.count ) {
NSLog(#"not nil");
}
NSLog(#"just before tested");
if ( matchesXML.count ) {
NSLog(#"tested");
I found the issue!
It wasn't crashing where I thought. It was a couple lines later. I was releasing an auto-released object. It must have been a left-over line from a previous organization of the code.
I removed the release and it's working perfectly again.
My guess is that you're not using the return statement you listed, but rather one that returns a different NSArray, only this one isn't retained. The first NSLog causes heap to be used, stomping on the array object.

why the tableview doesn't show binding data?

Here's my code of generating data
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[array initWithCapacity:20];
}
- (IBAction) readlog:(id)sender {
for (int i = 0; i < 20; i++) {
NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:[path stringByAppendingFormat:#"/%d.log",i]];
[array addObject:d];
}
}
- (IBAction) writelog:(id)sender {
for (int i = 0; i < 20; i++) {
NSMutableDictionary *d = [NSMutableDictionary dictionary];
NSString *name = [NSString stringWithFormat:#"testfile%d", i];
[d setObject:[NSDate date] forKey:#"date"];
[d setObject:[path stringByAppendingFormat:#"/%d.log", i] forKey:#"path"];
[d setObject:name forKey:#"name"];
[d writeToFile:[path stringByAppendingFormat:#"/%d.log", i] atomically:YES];
}
and I bind my tableview column with appdelegate.array with keypath name/path/date
but it doesn't show any data in the array.. is there anything wrong here?
Thanks!
You haven't created an array.
init methods, including NSMutableArray's initWithCapacity:, initialize an existing (freshly-created) instance. You haven't created one, so you're sending that initWithCapacity: message to nil, which means it has no effect.
You need to create the array, then initialize it, then assign it to your array variable, preferably all in the same line.
There's also the issue that your table view will have already asked for the array by the time you receive the applicationDidFinishLaunching: message. You don't have one yet, so it gets nothing; by the time you create one, it has already asked you for it and gotten its answer, and does not know that it should ask again.
Create your array in init or initWithCoder: (I believe you will need the latter if your app delegate is in a nib), and implement and use Key-Value-Coding-compatible accessor methods to fill the array with values. When you send yourself accessor messages, you'll cause KVO notifications that will tip off the table view that it needs to ask for the array again. Assigning directly to the instance variable will not cause this effect.
A couple of other thingsā€¦
You have three [path stringByAppendingFormat:#"/%d.log", i] expressions in two different methods. Don't repeat yourself. Move that to a method named something like logFileNameWithLogFileNumber: and send yourself that message to generate the filename. This will make the code both clearer and easier to maintain.
Finally, as a matter of style, you should not use stringByAppendingFormat: or stringWithFormat: to construct paths. Use stringByAppendingPathComponent: (in this case, together with stringWithFormat: to generate the filename). Clarity and pathname-separator-independence are virtues.

Test NSmutable array from plist before saving

I'm trying to made a cocoa app that read-write to a .plist file.
I can retrieve informations from the .plist, write into, but when a key (only with strings) is empty, the app don't write to the plist.
here a sample:
-
(IBAction)saveBoot:(id)sender {
NSString *errorDesc;
NSString *bootPath = #"/myplist.plist";
NSMutableDictionary *plistBootDict =
[NSMutableDictionary dictionaryWithObjects:
[NSMutableArray arrayWithObjects:
Rescan,
RescanPrompt,
GUI,
InstantMenu,
DefaultPartition,
EHCIacquire,
nil]
forKeys:[NSMutableArray arrayWithObjects:
#"Rescan",
#"Rescan Prompt",
#"GUI",
#"Instant Menu",
#"Default Partition",
#"EHCIacquire",
nil]];
NSData *plistBootData = [NSPropertyListSerialization
dataFromPropertyList:plistBootDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&errorDesc];
if (bootPath) {
[plistBootData writeToFile:bootPath atomically:NO];
}
else {
NSLog(errorDesc);
[errorDesc release];
}
}
#end
I think i need a loop to check if each key is empty or not (and remove it if empty),
but i've tried different (objectEnumerator, objectForKey:..etc) method whitout success.
If someone can help a beginner like me,
thanks in advance.
Ronan.
The problem is probably that because nil is the terminator for variable argument lists, so if, say, RescanPrompt is nil, the object array will only contain up until that part (so you can't "remove if empty" since it won't exist in the dictionary in the first place). You should probably construct your dictionary piece by piece; something like:
NSMutableDictionary *plistBootDict = [NSMutableDictionary dictionary];
if (Rescan)
[plistBootDisc setObject:Rescan forKey:#"Rescan"];
if (GUI)
[plistBootDisc setObject:GUI forKey:#"GUI"];
// etc
(Also, there's no reason to be using NSMutableArray or NSMutableDictionary if you're never going to be mutating them later.)