Convert CFStringRef to NSString - objective-c

I'm trying to retrieve some info from the main bundle:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
CFStringRef bundleVer = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
NSString *appVersion = (__bridge NSString *)bundleVer;
}
I can get the CFStringRef (in debug I can see the proper value associate to the variable), but when I try to cast it to NSString my appVersion variable has "null" value (previously it was nil).
What am I doing wrong?
I'm using ARC.
EDIT: It seems that I have problem with my project, every NSString object can't be assigned even with a simple static assignment, the value of the test variable is (null)
NSString *test = #"";

CFStringRef boundleVer = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
NSString *appVersion = (__bridge NSString *)(boundleVer);
Its working fien on my side,, it is returning "2"

I would use that code instead of your current one to fetch the same information:
NSString *_shortVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
or
NSString *_version = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"];
NOTE: I'm not sure which information you need eventually from the bundle.

Related

Populate an NSArray via an NSDictionary which is populated via a .plist

Here is the code:
-(void)viewDidLoad {
[super viewDidLoad];
//Verb data read, sorted and assigned to a dictionary
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:#"VerbDictionary" ofType:#"plist"];
NSDictionary *verbDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSArray *verbs = [verbDictionary allKeys];
NSArray *vSorted = [verbs sortedArrayUsingSelector:#selector(compare:)];
NSString *selectedVerb = [vSorted objectAtIndex:0];
NSArray *vArray = [verbDictionary objectForKey:selectedVerb];
self.verbArrayData = [[NSArray alloc] initWithArray:vArray];
}
Here is a screenshot of the Error message I'm getting:
(from https://plus.google.com/u/0/113629344949177123204/posts/SwHzXL6kvvJ)
The self.verbArrayData is not populating from the vArray. self.verbDataArray is nil and it shouldn't be.
I have tried this from scratch and I have done this before, actually, in the past, but via iOS 4 and Release/retain memory management. This is the first pure iOS 5 ARC app I have started.
Any ideas?
I figured it out... But I am not sure why it works. Self.verbArrayData is invalid. However, just verbArrayData works just fine. So, I changed self.verbArrayData = [[NSArray...] TO just verbArrayData = [[NSArray...] and it compiled and ran fine. So, self.verbArrayData is the getter not the setter in this case; I think. THanks - –

NSMutableArray always return null

My problem is when I read content of plist file in an NSMutableArray always return null
NSString *resourceDocPath = [[NSString alloc] initWithString:[[NSBundle mainBundle]bundlePath]] ;
// Create the new dictionary that will be inserted into the plist.
NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:#"walid" forKey:#"id"];
[nameDictionary setValue:#"555 W 1st St" forKey:#"lien"];
NSString *r = [NSString stringWithFormat:#"%#/download.plist", resourceDocPath];
NSLog(#"%#",r);
// Open the plist from the filesystem.
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:r];
NSLog(#"%#",plist);
if (plist == NULL)
{
plist = [NSMutableArray array];
}
[plist addObject:nameDictionary];
NSLog(#"%#",plist);
[plist writeToFile:r atomically:YES];
when I look in the plist file I found the data that I insert only one
can you help me please?
You're trying to access the application bundle rather than the documents directory, which can be accessed via NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Populator"];. The bundle cannot be modified, so the created array is never saved, hence why it is never loaded.
First you should not check for plist == null but check for plist == nil
Second searching for the download file should be changed into the following:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"download" withExtension:#"plist"];
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfURL:url];
Third:
I do not think a file with the extension of plist will return an Array.
It will probably represent an dictionary. Try creating an NSMutableDictionary instead of an array.

NSUserDafaults reading Root.plist got a nil value

This is the code in my AppDelegate:
NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:#"Settings.bundle"];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:#"Root.plist"];
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
NSArray *prefSpecifierArray = [settingsDict objectForKey:#"PreferenceSpecifiers"];
prefSpecifiersArray is setted to 0x0 < nil >. I really don't know how is it possible!
This is my Root.plist:
The values from settings.bundle do not actually load to NSUserDefaults until the user opens the settings for the first time. By default, they are off. Once the user opens the settings bundle, they will fill in for you.

Reading a string from a .plist file error

data = [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"INFO" ofType:#"plist"]];
name = [[data objectForKey:#"Name"]stringValue];
I am getting a SIGABRT error on when I try to give create name. All the names are alright. What could be wrong?
I have a INFO.plist file in my project. It has a row of type String. The value is Test.
Provided name is an NSString *, the following ought to work:
NSString *name = [data objectForKey:#"Name"];
NSDictionary's -objectForKey: returns the object, which will already be an NSString. (I'm not sure why you're calling -stringValue on it, but that could cause a crash or exception).

NSString is being returned as 'null'

I have this simple method for returning the file path. I am passing the file name as argument. Then when I call this method this method returns 'null' if running on device but works fine on simulator. Is there anything I am doing wrong?
-(NSString*) getFilePathForFile:(NSString*)fileName
{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [array objectAtIndex:0];
NSString *temp = [documentsDirectory stringByAppendingPathComponent:fileName];
return temp;
}
NSString *path = [self getFilePathForFile:#"settingInfo.plist"]
Any help would be appreciated.
Thanks
It sounds like some sort of memory management problem. Have you tried to use "Build and Analyze" to see if Xcode can pick up any memory problems?
Otherwise, try this and see if you get something non-null:
NSString *path = [[self getFilePathForFile:#"settingInfo.plist"] retain];
The only way that method can fail and not throw an exception is if the array returned by NSSearchPathForDirectoriesInDomains is nil. Check that a valid array is being returned.