保存和检索UIColors到Core Data的最佳方法 [英] Best way to save and retrieve UIColors to Core Data
问题描述
我可以想到几种方法,例如将每个颜色组件保存为float,将数组保存为transformable。从模式图像保存颜色可能会稍微复杂一些,但我想你可以将图像的名称保存为字符串,并将其插入到创建代码。
I can think of a few ways, for instance saving each color component as a float, saving an array as transformable. Saving a color from pattern image could be slightly more complicated, but I guess you could save the name of the image as a string and insert that into creation code.
我在正确的轨道上,那么一般来说,最好的和最有效率的工作是什么?
Am I on the right track, else what is the best and most efficient to do this task in general?
推荐答案
您可以将 UIColor
转换为 NSData
NSData *theData = [NSKeyedArchiver archivedDataWithRootObject:[UIColor blackColor]];
那么您可以将它转换回您的 UIColor
object:
then you can convert it back to your UIColor
object:
UIColor *theColor = (UIColor *)[NSKeyedUnarchiver unarchiveObjectWithData:theData];
UPD。:
在关于存储数据的注释中回答您的问题,最简单的方法是将它存储在 NSUserDefaults
:
Answering your question in comments about storing the data, the most simple way is to store it in NSUserDefaults
:
NSData *theData = [NSKeyedArchiver archivedDataWithRootObject:[UIColor blackColor]];
[[NSUserDefaults standardUserDefaults] setObject:theData forKey:@"myColor"];
,然后检索:
NSData *theData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *theColor = (UIColor *)[NSKeyedUnarchiver unarchiveObjectWithData:theData];
这篇关于保存和检索UIColors到Core Data的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!