nskeyedarchiver

Unarchiving UIImageView with subviews

为君一笑 提交于 2019-12-01 23:26:15
I'm going a bit crazy trying to archive and unarchive a UIImageView which has number of subviews which are custom views derived from UIImageView. Here's what I've done: Add a category to the project to allow for the fact that UIImage does not conform to NSCoding: #import "UIImage-NSCoding.h" #define kEncodingKey @"UIImage" @implementation UIImage(NSCoding) - (id)initWithCoder:(NSCoder *)decoder { if ((self = [super init])) { NSData *data = [decoder decodeObjectForKey:kEncodingKey]; self = [self initWithData:data]; } return self; } - (void)encodeWithCoder:(NSCoder *)encoder { NSData *data =

UI_19 数据持久化(本地存储)

陌路散爱 提交于 2019-12-01 19:24:36
一、数据持久化概述 数据持久化就是数据的永久存储。其本质是将数据保存为文件,存到程序的沙盒中。 1、 数据持久化的方式 1.1 writeToFile:简单对象写入文件 1.2 NSUserDefaults: 应用程序偏好设置 1.3 Sqlite: 轻量级关系型数据库,不能直接存储对象 (NSData 除外 ) ,需要用到一些 SQL 语句,先将复杂对象归档 ( 对象 ->NSData) 1.4 CoreData: 对象型数据库,实质是将数据库的内部存储细节封装 1.5 Plist 文件 2、 应用程序沙盒 每一应用程序都有自己的应用沙盒 ,沙盒的本质就是一个文件夹,名字是随机分配的。 与其他应用程序沙盒隔离,应用程序本身只能访问自己沙盒的数据。 (iOS8+ 对沙盒之间的访问部分开放 ) 2.1 应用程序包 (.app) 包含了应用程序中所用到的所有资源文件和可执行文件 (Base on Unix)。iOS8时,app不存储在沙盒中,有单独的文件夹存储所有程序的app包。 2.2 HomeDirectory Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录 tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件

Encoding NSAttributedString Throws Error

ぐ巨炮叔叔 提交于 2019-12-01 12:28:08
问题 Based on the accepted answer to this question I wrote the following code: NSData* somedata; somedata=[NSKeyedArchiver archivedDataWithRootObject:ts]; where ts is an NSAttributedString that is populated with some text and some attributes (colors, in this case). When I execute this code, I receive this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x6eb5b90' I'm new to the

Saving an NSArray of custom objects

心不动则不痛 提交于 2019-12-01 11:08:29
I've created a subclass of UIImage (UIImageExtra) as I want to include extra properties and methods. I have an array that contains instances of this custom class.However when I save the array, it appears the extra data in the UIImageExtra class is not saved. UIImageExtra conforms to NSCoding, but neither initWithCoder or encodeWithCoder are called, as NSLog statements I've added aren't printed. My method to save the array looks like this: - (void)saveIllustrations { if (_illustrations == nil) { NSLog(@"Nil array"); return; } [self createDataPath]; //Serialize the data and write to disk

NSKeyedArchiver archiveRootObject keeps returning NO

我的未来我决定 提交于 2019-12-01 08:32:14
I've spent the past few hours trying to figure this out, but I've ran out of ideas. All I'm trying to do is archive an object, but the method archiveRootObject keeps on returning NO Here's my code: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cacheDirectory = [paths objectAtIndex:0]; cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MyAppCache"]; NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:@"archive.data"]; if(![[NSFileManager defaultManager] fileExistsAtPath:fullPath]){ [[NSFileManager

NSKeyedArchiver archiveRootObject keeps returning NO

守給你的承諾、 提交于 2019-12-01 06:53:36
问题 I've spent the past few hours trying to figure this out, but I've ran out of ideas. All I'm trying to do is archive an object, but the method archiveRootObject keeps on returning NO Here's my code: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cacheDirectory = [paths objectAtIndex:0]; cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MyAppCache"]; NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:@

Need to archive CLLocation data

梦想的初衷 提交于 2019-12-01 05:47:05
I have an array of CLLocation data that I would like to archive. Should the NSUserDefaults system be used? Otherwise, how best to archive the CLLocation data? UserDefaults can only store certain types of data, and should be used to store user preference information, not arbitrary application state data. To quote the Apple docs : The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of

Writing and reading custom object to file IOS

大城市里の小女人 提交于 2019-12-01 00:48:35
i have this object. @interface SeccionItem : NSObject <NSCoding> { NSString * title; NSString * texto; NSArray * images; } @property (nonatomic,strong) NSString * title; @property (nonatomic,strong) NSString * texto; @property (nonatomic,strong) NSArray * images; @end With this implementation @implementation SeccionItem @synthesize title,texto,images; - (void) encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:title forKey:@"title"]; [encoder encodeObject:texto forKey:@"texto"]; [encoder encodeObject:images forKey:@"images"]; } - (id)initWithCoder:(NSCoder *)decoder { title = [decoder

Writing and reading custom object to file IOS

隐身守侯 提交于 2019-11-30 19:15:57
问题 i have this object. @interface SeccionItem : NSObject <NSCoding> { NSString * title; NSString * texto; NSArray * images; } @property (nonatomic,strong) NSString * title; @property (nonatomic,strong) NSString * texto; @property (nonatomic,strong) NSArray * images; @end With this implementation @implementation SeccionItem @synthesize title,texto,images; - (void) encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:title forKey:@"title"]; [encoder encodeObject:texto forKey:@"texto"];

Simple persistent storage in Swift

放肆的年华 提交于 2019-11-28 09:07:49
I have an array of objects each with a number of properties. Here is some sample data taken by looping through the array of objects: Name = Rent Default Value 750 This Months Estimate = 750 Sum Of This Months Actuals = 0 Risk Factor = 0.0 Monthly Average = 750.0 -------------- Name = Bills Default Value 250 This Months Estimate = 170 Sum Of This Months Actuals = 140 Risk Factor = 0.0 Monthly Average = 190.0 -------------- Name = Food Default Value 240 This Months Estimate = 200 Sum Of This Months Actuals = 95 Risk Factor = 0.0 Monthly Average = 190.0 -------------- Name = Lunches Default Value