Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-06-2011, 05:24 AM   #1 (permalink)
Scar
 
Join Date: Dec 2010
Location: Palestine
Age: 23
Posts: 6
Mumen Shabaro is on a distinguished road
Send a message via MSN to Mumen Shabaro
Question Problem With NSMutableArray and NSDictinary

Hi there,

i have the following problem i can't solve.

i have a NSMutableArray that contains about 12 NSDictionaries and i wanna save it to a plist file.

this is the code that save it
Code:
-(void)writeToPlist:(NSString*)plistFileName array:(NSMutableArray*)arrayToWrite
{
	NSString *FileNamePath = [[NSString alloc] initWithFormat:@"%@.plist",plistFileName];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
	[arrayToWrite writeToFile:appSettingsPath atomically:YES];
	[FileNamePath release];
}
and this is the code that read the file after saving it

Code:
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
			NSString *documentsDirectory2 = [paths1 objectAtIndex:0];
			NSString *appSettingsPath2 = [documentsDirectory2 stringByAppendingPathComponent:@"ImageFileData.plist"];
			imageArray = [[NSMutableArray alloc] initWithContentsOfFile:appSettingsPath2];
but it doesn't work, every time i start the app i got the count of imageArray = 0 . and i checked that there is an information that wrote to the plist file.

i print the result of this statement

Code:
[arrayToWrite writeToFile:appSettingsPath atomically:YES];
NSLog(@"%@",arrayToWrite);
i got this
Code:
(
        {
        "\U0627\U0644\U0639\U064a\U0646" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/1.gif";
    },
        {
        "\U0627\U062a\U062d\U0627\U062f \U0643\U0644\U0628\U0627\U0621" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/2.png";
    },
        {
        "\U0627\U0644\U0634\U0628\U0627\U0628" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/3.gif";
    },
        {
        "\U0627\U0644\U0638\U0641\U0631\U0629" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/4.png";
    },
        {
        "\U0627\U0644\U0648\U0635\U0644" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/5.jpg";
    },
        {
        "\U0627\U0644\U062c\U0632\U064a\U0631\U0629" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/6.gif";
    },
        {
        "\U0628\U0646\U064a \U064a\U0627\U0633" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/7.png";
    },
        {
        "\U0627\U0644\U0648\U062d\U062f\U0629" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/8.gif";
    },
        {
        "\U200e\U0627\U0644\U0634\U0627\U0631\U0642\U0629" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/9.png";
    },
        {
        "\U0627\U0644\U0646\U0635\U0631" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/10.gif";
    },
        {
        "\U062f\U0628\U064a" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/11.jpg";
    },
        {
        "\U0627\U0644\U0623\U0647\U0644\U064a" = "/var/mobile/Applications/5756C245-756E-496C-A731-B23ADD2AA96C/Documents/12.gif";
    }
)
so what is the solution if u know it. thx in advance.
Mumen Shabaro is offline   Reply With Quote
Old 01-06-2011, 06:54 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Mumen Shabaro View Post
Hi there,

i have the following problem i can't solve.

i have a NSMutableArray that contains about 12 NSDictionaries and i wanna save it to a plist file.

this is the code that save it
Code:
-(void)writeToPlist:(NSString*)plistFileName array:(NSMutableArray*)arrayToWrite
{
	NSString *FileNamePath = [[NSString alloc] initWithFormat:@"%@.plist",plistFileName];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
	[arrayToWrite writeToFile:appSettingsPath atomically:YES];
	[FileNamePath release];
}
and this is the code that read the file after saving it

Code:
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
			NSString *documentsDirectory2 = [paths1 objectAtIndex:0];
			NSString *appSettingsPath2 = [documentsDirectory2 stringByAppendingPathComponent:@"ImageFileData.plist"];
			imageArray = [[NSMutableArray alloc] initWithContentsOfFile:appSettingsPath2];
but it doesn't work, every time i start the app i got the count of imageArray = 0 . and i checked that there is an information that wrote to the plist file.

i print the result of this statement

Code:
[arrayToWrite writeToFile:appSettingsPath atomically:YES];
NSLog(@"%@",arrayToWrite);

so what is the solution if u know it. thx in advance.

Your code looks reasonable, and should work. The only thing I can think of is that to save an array or dictionary to a plist, every item in the object has to be a "plist object" (the list of valid object types is described in the docs.) If you put a non-plist object anywhere in the object graph, the save fails.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-06-2011, 07:42 AM   #3 (permalink)
Scar
 
Join Date: Dec 2010
Location: Palestine
Age: 23
Posts: 6
Mumen Shabaro is on a distinguished road
Send a message via MSN to Mumen Shabaro
Default

hi there, i found the problem, when i call the method
Code:
-(void)writeToPlist:(NSString*)plistFileName array:(NSMutableArray*)arrayToWrite
i was send to it the string "plistFileName" with the plist extinction. which is i'm alreday put it in the method. so the file name will be "imageFile.plist.plist"

becouse of that i wasn't able to read from it. :P :P :P

thx for everyone read my thread and thx u Duncan C for your reply
Mumen Shabaro is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, nsdictionary, nsmutablearray, save file, sdk 4

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 363
7 members and 356 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:32 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0