Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 28, 2009, 10:08 AM   #1
dova
macrumors newbie
 
Join Date: Oct 2009
Write/Read plist in <data> format

hello,

i would like to create a plist, then write and read <data></data> format for a key in the plist. The rest of keys are in string format.
The following code does what i want but it writes and reads all key in <string></string> instead of data format.
How can i get it to work?
Thanks!

write:
PHP Code:
/* write plist */
    
NSString *plistPath = @"/Users/dova/Desktop/data.plist";
    
NSMutableDictionary *plistDict = [[NSMutableDictionary allocinit];
    [
plistDict setObject:info forKey:@"name"];
...
    [
plistDict writeToFile:plistPath atomically:YES]; 
read:
PHP Code:
/* read plist */
    
NSString *plistPath = @"/Users/dova/Desktop/data.plist";
    
NSMutableDictionaryplistDict = [[NSMutableDictionary allocinitWithContentsOfFile:plistPath];
    
NSData *value;
    
value = [plistDict objectForKey:@"name"]; 

Last edited by dova; Oct 28, 2009 at 10:19 AM.
dova is offline   0 Reply With Quote
Old Oct 28, 2009, 10:14 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
What type of object is info? If it's a NSString then it'll get written as a string. You would need to convert (not cast) to NSData to get what you want.
robbieduncan is offline   0 Reply With Quote
Old Oct 28, 2009, 10:27 AM   #3
dova
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
The info is a string type (I got it to write and read it).
The next key is idata (setObject:idata forKey:@"data") in the string type, and i would like to convert it to data format for both read and write. but i do not know how.
dova is offline   0 Reply With Quote
Old Oct 28, 2009, 10:32 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Read the documentation.
robbieduncan is offline   0 Reply With Quote
Old Oct 28, 2009, 10:58 AM   #5
dova
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
i got it to encode in the data format:
PHP Code:
NSData datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
[
plistDict setObject:datax forKey:@"data"]; 
how is it with decode to read? i do not found any info for decoding in the documentation.
Thank you
dova is offline   0 Reply With Quote
Old Oct 28, 2009, 11:00 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by dova View Post
i got it to encode in the data format:
PHP Code:
NSData datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
[
plistDict setObject:datax forKey:@"data"]; 
how it is with decode? i do not found any info for decoding in the documentation.
Thank you
Are you serious? On the same ****** page I linked to above is this, amazingly obviously named, clearly documented init method.

In all seriousness: if you couldn't see that give up now.
robbieduncan is offline   0 Reply With Quote
Old Oct 28, 2009, 11:32 AM   #7
dova
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Thank you!
i got it to decode:
PHP Code:
NSData *datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
NSString *dec_data = [[NSString allocinitWithData:datax encoding:NSUTF8StringEncoding]; 
NSLog (@"decode: %@\n"dec_data); 
dova is offline   0 Reply With Quote
Old Oct 28, 2009, 11:57 AM   #8
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
The real question is why? This is not really any more secure than just saving it as a string...
robbieduncan is offline   0 Reply With Quote
Old Oct 28, 2009, 12:32 PM   #9
dova
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
I use the data format to store the encrypted key using openssl. The key is encrypted as plain text:
Quote:
2f%•ae˝dbG.oo]!?fi?√π–F?G—Yo@5√;˝?…?8X?ŒkOT
Just to make sure that i won't have problem with some weird characters i need to convert it in the data format:
Quote:
<data>
MmYlwqzigKJhw4Zly53DrWRiRy5vb13DiiHDkgTDje+sgRriiJrPgOKAk8O5RhxH4oCU
w7ZZb8KsQDXDhOKImsOPw4g7w6TLncOBHcOo4oCmw4HDiQU4w4lYw6ocxZJrw5pPwqHD
rlQATAfDkjx4bDfiiJrDt28Twq9vw6R64oC64omIw7jigJN64oCdw4QfDSrDtTA3B8ub
4oirw6fDssOVw7Q+wqNdZG9wFDx9a1Uzy4ZOw40oPWzCoMuGw4zCriHDiRJtxpLDqQ==
</data>
dova is offline   0 Reply With Quote
Old Oct 28, 2009, 03:34 PM   #10
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Ah, OK, well that sounds sensible. I would check/ensure that the encoding you are choosing can represent all the characters you have though...
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Defaults write not working? (editing trackpad PLIST in Terminal) changes don't apply Ancandis OS X 0 Oct 8, 2013 02:47 AM
VMware Fusion 5 - Shared Folder (Read/Write) - Yet no write permissions in VM altjx Windows, Linux & Others on the Mac 2 Aug 29, 2013 09:19 AM
iPad: Read/write data via wifi Stephen Valente iPad 1 Dec 2, 2012 11:17 PM
How to read specific key value from plist file saqibjaan Mac Programming 3 Sep 2, 2012 09:27 PM
How to read plist value by AppleScript? mikezang Mac Programming 12 Aug 28, 2012 09:54 AM

Forum Jump

All times are GMT -5. The time now is 03:35 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC