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 10-19-2011, 12:41 PM   #1 (permalink)
Rko
Registered Member
 
Join Date: Jun 2010
Posts: 63
Rko is on a distinguished road
Default Saving Dictionary with NSAttributed String into a PList or into NSUserDefaults

Hi,

I've a Dictionary with an NSAttributtedString

Code:
{
        9 =     {
            9x0 =         {
                UserID = 123;
                formattedMessage = "Hi... How are you ?{\n    CTForegroundColor = \"<CGColor 0x601e460> [<CGColorSpace 0x6010020> (kCGColorSpaceDeviceGray)] ( 0 1 )\";\n    NSFont = \"CTFont <name: Cochin-BoldItalic, size: 17.000000, matrix: 0x0>\\nCTFontDescriptor <attributes: <CFBasicHash 0x6070640 [0x11f8400]>{type = mutable dict, count = 1,\\nentries =>\\n\\t1 : <CFString 0xf4d5c4 [0x11f8400]>{contents = \\\"NSFontNameAttribute\\\"} = <CFString 0x60707f0 [0x11f8400]>{contents = \\\"Cochin-BoldItalic\\\"}\\n}\\n>\";\n}";
                isDeleted = 0;
                isEdited = 0;
                lastModifiedDate = "2011-10-19 17:27:38 +0000";
                message = "Hi... How are you ?";
            };
        };
    }
I tried Saving this Dictionary as a Plist in the Documents folder
and also,
save the dictionary in NSUserDefaults.
Neither of it worked.
Both methods work if the attributed string in the dictionary is removed.

Code used to save to Plist in Documents folder :

Code:
	
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:@"%@/ChatHistory.plist",aDocumentsDirectory];
BOOL aStatus = [aDictionary writeToFile:aFilePath atomically:YES];
Code used to save Dictionary in NSUserDefaults :

Code:
	
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:aDictionary forKey:@"ChatHistory"];
Someone please help. Am I missing something ?
Rko is offline   Reply With Quote
Old 10-20-2011, 12:29 AM   #2 (permalink)
Rko
Registered Member
 
Join Date: Jun 2010
Posts: 63
Rko is on a distinguished road
Default

boosting it up... any solutions ?
Rko is offline   Reply With Quote
Old 10-20-2011, 12:38 AM   #3 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You can't. NSAttributedString isn't a property list object. Here's an excerpt from the docs on NSUserDefaults:

Quote:
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
NSAttributedString doesn't inherit from NSString, so it's a no go. You'll have to archive it to NSData before saving.
baja_yu is offline   Reply With Quote
Old 10-20-2011, 01:30 AM   #4 (permalink)
Rko
Registered Member
 
Join Date: Jun 2010
Posts: 63
Rko is on a distinguished road
Default

Thanks for the lead

Really new to archiving.
Tried :

Code:
NSData *aStringData = [NSKeyedArchiver archivedDataWithRootObject:self.attributedMessage];
app crashed with :
Quote:
-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x604e3d0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x604e3d0'
Can you please help me out with this ?
Rko is offline   Reply With Quote
Old 10-20-2011, 04:04 AM   #5 (permalink)
Rko
Registered Member
 
Join Date: Jun 2010
Posts: 63
Rko is on a distinguished road
Default

An interesting Implementation.
Guess this is a solution :

https://devforums.apple.com/message/340283#340283

will explore this now..
Rko is offline   Reply With Quote
Reply

Bookmarks

Tags
ios, nsattributedstring, nsuserdefaults, plist

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: 408
15 members and 393 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, n00b, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

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