Cocos2d: .plist file giving me a SIGABRT error - ios

Hello I am making a side scrolling cocos2d app. I am using a .plist file for most of the data in my game. When I run the code it immediately gives me a SIGABRT error. I am new to objective c and cocos2d and I am not experienced with .plist files. This is the .plist file.
This is the code that I am pretty sure is causing the problem.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"GameData.plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:finalPath];
NSMutableArray* characterArray = [NSMutableArray arrayWithArray:[plistData objectForKey:#"Characters"]];
NSDictionary *theCharacterDict = [NSDictionary dictionaryWithDictionary: [characterArray objectAtIndex:0]];
NSDictionary* characterDict = [NSDictionary dictionaryWithDictionary:[theCharacterDict objectForKey:#"PlayerProperties"]];
character = [Character createWithDictionary:characterDict];
[self addChild:character z:kCharacterLevel];
I do not know if this is the code that is causing the problem. I will post more code if needed.

Okay I don't know if this is what is causing the error, but I immediately spotted an error in your code. NSMutableArray* characterArray The asterisk should be immediately before characterArray.
As I read further, there are several minor errors in the code you posted. Asterisks are in the wrong places are there are too many spaces in certain areas. My suggestion is to read through your code line by line to help catch such errors.

Related

iOS - Issue with reading from a plist

I'm having an issue with reading from a plist I can't seem to figure.
My plist list looks like this (note i've simplified it for examples sake):
Then I'm reading code like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"ContactDetails" ofType:#"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary allKeysForObject:#"Name1"];
Any ideas - the issue i keep facing is allmyKeys shows up as containing 0 objects. On debugging myDictionary is correctly populated so not sure why it doesn't work.
Many Thanks
The problem is with this line:
NSArray* allmyKeys = [myDictionary allKeysForObject:#"Name1"];
#"Name1" is a key in your dictionary, not a value. Even if it were a value, the string #"Name1" is a different object from the string with the same value in myDictionary, so this call would not do what you expect.
You probably want to access the dictionary by doing
NSDictionary *userDetails = myDictionary[#"Name1"];

Read a plist from website

I am incorporating an image gallery into my project. It currently reads from a local plist file, and I need to change it to read from a web server. I've read a number of ways to do this, but can't make it work in my context. This seems maddeningly simple.
Below is my code:
NSString *plistPath =
[[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfFile:plistPath];
You can view my plist here
This is what I've tried to no avail:
//get image URLs
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSDictionary *imagePaths = [[NSDictionary alloc] initWithContentsOfURL:plistPath];
I'd greatly appreciate anyone's help.
Try this and it should be work :
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfURL:plistPath];
NSLog(#"%#", imagePaths);
The URL you provide serves a plist that contains an array.
Your code tries to read it as a dictionary.
Just change your code to read it as an array and you'll be one step closer.
But as others have said in the comments, you can't download a file (plist or otherwise) to your bundles directory. You have to download it to your Documents directory or similar.

Objective-C: Reading from a .rtf file into an array

I've looked around a bit and tried a few things, but none have really worked. What I'm trying to do is create a NSArray of NSStrings, with each array value corresponding to one line from the Rich Text File I'm referencing. At first I tried this:
NSArray* data = [[NSString stringWithContentsOfFile:#"relevantFile.rtf" encoding:4 error:nil] componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
I've also tried this, because I found it in the iOS developer library:
NSArray* data = [[NSArray alloc] initWithContentsOfFile:#"relevantFile.rtf"];
However, neither of these has worked for me. A few lines later in the code, in order to diagnose errors, I have the following code:
for(int i = 0; i < [data count]; i++)
{
NSLog(#"%#", [data objectAtIndex: i]);
}
...for which NSLog is printing "(null)". I'm not entirely sure what I'm doing wrong here -- should I be using mutable strings or arrays, or is there some better way to go about this that I don't know about?
That first line you posted should do it. My guess would be that it's not finding the file. Not specifying an absolute path, the app will look in the current directory which is probably NOT where the file is.
If the file is a resource that is compiled into your app bundle, you can use the following code to obtain the path to it:
NSString* path = [[NSBundle mainBundle] pathForResource: #"relevantFile" ofType: #"rtf"]
NSArray* data = [[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];

Get and update plist value

I created a plist titled "appData.plist" in my supporting files section of Xcode. I have a key called "keyone" and am trying to get the value of it. I'm trying to log the value to console with NSLog but its not working.
How do you do this? I've searched google and there are a lot of way but none work for me.
NSString *myFile = [[NSBundle mainBundle] pathForResource: #"appData" ofType: #"plist"];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:myFile];
NSString *viewCover = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"keyone"];
This is what i've tried but the whole thing ends in errors, I can't log it and I get an 'unusable variable' error.
The objectForInfoDictionaryKey method retrieves a value from your app's Info.plist file, which isn't what you want -- you want to retreive it from your dictionary myDict. You mentioned that the value is actually a boolean, so replace the last line with
BOOL viewCover = [[myDict objectForKey:#"keyOne"] boolValue];

iOS5 using ARC: Implicit conversion of int to NSDictionary

i´m using ARC to update my old project.
I have a "Filehelper" class in which i use c-functions e.g. for methods i need in almost every projects. (eg.load plist, etc..)
ViewController
NSDictionary* dictionary = getDictionaryFromPlistWithName(#"TestFile", #"plist");
Filehelper.m
#pragma Returns content of plist as NSDictionary
NSDictionary* getDictionaryFromPlistWithName(NSString* fileName, NSString* pathExtension) {
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:pathExtension];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
if(fileExists){
NSDictionary* dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
return dictionary;
}
else{
[NSException raise:#"File not found" format:#"path %#", path];
return nil;
}
}
I get this error:
*error: Automatic Reference Counting Issue: Implicit conversion of 'int' to 'NSDictionary ' is disallowed with ARC
Any ideas how to fix it to use it with iOS 5?
I´ve read something that i need to use (__bridge NSDictionary*), but that didn´t help.
PS.
What´s your workflow for classes which you already need? Use C-functions, too?=
What´s the best way?
The most likely problem here is that you forgot to #include your header file. I bet there are further warnings that you're ignoring, particularly one that says something like "unknown signature for getDictionaryFromPlistWithName, assuming it returns int". Read through your warnings and never ignore them (I strongly recommend -Werror for all ObjC projects).

Resources