count items in plist array - ios

I am having difficult accessing the count of a chosen plist. In the previous view controller, the user selects from one of several buttons, each one corresponding to a particular plist. That plist is sent to the current vc as NSString * chosenPlist. I have NSLogged to see that the documents directory and the plistpath seem to be correct, but the array that I want to count is still 0.
How can I count the number of items in the chosen plist?
NSString * chosenPlistPlus = [chosenPlist stringByAppendingString:#".plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"The documentsdirectory is %#", documentsDirectory);
NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:chosenPlistPlus];
NSLog(#"MyPlistPath = %#", myPlistPath);
NSArray *arr = [NSArray arrayWithContentsOfFile: myPlistPath];
//Trying to count the items in the plist, which is an array of dicts.
int count = arr.count;
NSLog(#"Count is %i", count);
here is a log:
2013-11-16 21:13:07.037 GlobalHistoryRegents[70407:70b] The documentsdirectory is /Users/username/Library/Application Support/iPhone Simulator/7.0.3/Applications/896EF347-A35E-40E2-9BE1-8CFAC5303347/Documents
2013-11-16 21:13:07.037 GlobalHistoryRegents[70407:70b] MyPlistPath = /Users/username/Library/Application Support/iPhone Simulator/7.0.3/Applications/896EF347-A35E-40E2-9BE1-8CFAC5303347/Documents/methodologyQuestions.plist
2013-11-16 21:13:07.038 GlobalHistoryRegents[70407:70b] Count is 0

try using,
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:myPlistPath];
NSArray *arr =dictionary[#"Root"];

Related

NSArray Getting Out Of Order

I use a PLIST to populate a TableView in my app. I want the TableView to be in order of how items were added, due to needing a good way to edit it later on, so the indexPath.row will match up with the indexPath of the PLIST. I get the TableView populated by going from the PLIST to NSDictionary to NSArray. The issue is that the TableView keeps listing everything in alphabetical order and not the order it was listed
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:self.selectedCountry];
_plist = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath];
_content = [_plist allKeys];

View Image array from web folder in iOS

I want to be able to view images from a web folder inside my iPhone app. I know how to view the images with a specific url (i.e. www.mywebsite.com/image.jpg). That's easy. I just don't know how to asynchronously load an array. Basically I need to view images with a specific sequence (i.e. mywebsite.com/image_001.jpg, image_002.jpg, image_003.jpg, etc). There may be 10 or 100 images in a folder with that sequence. How do I let my app load images with a sequence?
Following code will sort images of this file format "imageName_number.jpg"
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *webDir=[documentsDirectory stringByAppendingPathComponent:#"WebFolder"];
NSFileManager *filemgr;
NSMutableArray *fileNum;
fileNum=[[NSMutableArray alloc]init];
filemgr = [NSFileManager defaultManager];
//This will give all files of your web directory you can uncomment to get dynamically
//NSArray *filelist = [filemgr contentsOfDirectoryAtPath: webDir error: nil];
// this is just example shows how unsorted will be used to sort image no you can comment this line
NSArray *filelist=[[NSArray alloc]initWithObjects:#"abc_001.jpg",#"def_005.jpg",#"abc_002.jpg",#"abc_0103.jpg",#"abc_0010.jpg",#"abc_008.jpg", nil];
int count = (int)[filelist count];
NSMutableDictionary *dictFileNumWithPath=[[NSMutableDictionary alloc]init];
NSString *imageSeprator=#"_";
for (int i = 0; i < count; i++){
NSString *imageName=[filelist objectAtIndex: i];
if (!([imageName rangeOfString:imageSeprator].location == NSNotFound)) {
NSRange startRange = [imageName rangeOfString:imageSeprator];
NSRange endRange = [imageName rangeOfString:#".jpg"];
NSRange searchRange = NSMakeRange( startRange.location+1, endRange.location-endRange.length);
NSString *strNum= [imageName substringWithRange:searchRange];
NSString *webPath=[webDir stringByAppendingPathComponent:imageName];
[dictFileNumWithPath setObject:[NSNumber numberWithInt:[strNum intValue]] forKey:webPath];
}
}
NSArray *sortedKeysFilePathArray =
[dictFileNumWithPath keysSortedByValueUsingSelector:#selector(compare:)];
NSLog(#"%#", sortedKeysFilePathArray);

My plist is not updated when i close my application and open it again

Please,
I created a plist with some Keys and values,
And then I tried to add new item to my Plist and save it.
After that i close my App and open it again, but i don't see my new items on my Table View.
So I don't understand where is my wrong !!!
Please Help!!!
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingString:#"MyProfile.plist"];
NSString *error=nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:MyProfile format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if (plistData){
[plistData writeToFile:plistPath atomically:YES];
}
else{
NSLog(#"Error in save Data:%#",error);
}
self.profileInfo = [[NSMutableDictionary alloc]initWithContentsOfFile:filePathPlist];
The values in self.profileInfo is not mutable, if the value for a specific key is a array, it will be NSArray not NSMutableArray.
Let's talk about the code below:
self.names = [self.profileInfo objectForKey:#"Names"];
Although you declare the property names as a NSMutableArray, the instance you get here is still a NSArray. So you should send mutableCopy method to id to get a mutable array:
self.names = [[self.profileInfo objectForKey:#"Names"] mutableCopy];
BTW:
You can also save self.profileInfo to file directly using [self.profileInfo writeToFile:path atomically:YES].

Making array from plist containing arrays

I have this plist file
I need to make an array containing the arrays in the plist, so the array should have the values "numbers" & "colors" .
I tried this code but didn't work
- (NSString *) dataFilePath {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:#"groups.plist"];
}
- (void) readPlist {
NSString *path = [self dataFilePath];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *tempArray = [dict objectForKey:#"Root"];
NSLog(#"%#", tempArray);
}
calling the readPlist from ViewDidLoad Method
any idea how to do it ?
Instead of
NSArray *tempArray = [dict objectForKey:#"Root"];
NSLog(#"%#", tempArray);
use
NSArray *tempArray = [dict allKeys];
NSLog(#"%#", tempArray);
Note that the order of the elements in the array is not defined, see the documentation of NSDictionary,
"Root" is just a label from Xcode.
Ensure that the groups.plist file exists in the Documents Directory and not in your Bundle Directory.
Ensure that the dict is not nil and is actually a dictionary object as you expect.
Have you tried [dict objectForKey:#"numbers"]?

IOS: problem to synchronize nsarray with string

I have a NSArray in this way
myArray[0] = [string1, string2, string3, string4, mySecondArray, string5]; (at 0 position)
I write this array inside a txt file in this way
NSString *outputString = #"";
for (int i = 0; i< myArray.count; i++){
outputString = [outputString stringByAppendingString:[[[myArray objectAtIndex:i ] componentsJoinedByString:#"#"] stringByAppendingString:#";"]];
}
NSLog(#"string to write = %#", outputString);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Text.txt"];
NSError *error;
[outputString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
then the result of NSLog is = (position 0 of myArray) (mySecond array is empty)
one#two#three#four#(
)#five;
I want to know:
Why the array wrap?
When I'll go to read this string how can I know that it's mySecondArray?
When you message componentsJoinedByString: on an NSArray object, it calls description on each of its objects and concatenates them in order. For NSString objects, they are the strings themselves. The array wraps because of the way the description method has been implemented.
As for identifying the array while you are reading the string back, I don't think it is possible. You should consider writing the array to the file rather i.e.
[[myArray objectAtIndex:0] writeToFile:filePath atomically:YES];
or
[myArray writeToFile:filePath atomically:YES];
depending on the requirement. This way you will be able to read the elements back properly.

Resources