2012-04-19 75 views
4

我努力將幾個位置保存到plist文件供以後使用, 經過一番Google搜索後發現CLLocation本身的數組本身不能保存,所以我想知道一個方法來做到這一點。我在想幾個類將單個CLLocation對象「序列化」/「deserilize」到NSDictionary中,然後將這些NSDictionaries的數組存儲到plist文件中,但我想知道是否可以有更好的方法/更聰明/可靠的方式來實現這一點。保存CLLocation到plist

在此先感謝。

編輯:

這是我使用以將數據保存在plist中功能(c_propertyName需要從應答的代碼)

- (void) addLocation { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"/Locations.plist"]; 

     NSArray *keys = [curLocation c_propertyNames]; 
     NSDictionary *dict = [curLocation dictionaryWithValuesForKeys:keys]; 

     [dict writeToFile: path atomically:YES]; 
    } 

EDIT 2 - 解:

確定,我已經想通了。下面,我已經發布了一個解決方案來解決我自己的問題。

回答

0

經過幾個小時的搜索我已經想出了整個場景。 在這裏你有幾個解決方案;首先是更「骯髒」,因爲這是我第一次提出,而第二個是更優雅。無論如何,我會離開他們,因爲他們都可以派上用場。

S 0呂T I O 4 N - 1

由於mit3z我可以把拼在一起,以找出一個解決方案的幫助。

正如他指出的,可以實現此方法插入NSObject的類別:

- (NSArray *)c_propertyNames; 

(看他這部分的代碼響應,並進一步有關它的更多細節)

這給我冒昧地做這樣的事情:

- (void) addLocation { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"/Locations.plist"]; 

    NSArray *keys = [curLocation c_propertyNames]; // retrieve all the keys for this obj 
    NSDictionary *values = [self.curLocation dictionaryWithValuesForKeys:keys]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    for(NSString *key in keys) { 
     NSString *aaa = [NSString stringWithFormat:@"%@", (NSString *)[values valueForKey:key]]; 
     [dict setValue:aaa forKey:key]; 
    } 

    [dict writeToFile:path atomically:YES]; 
} 

需要for循環此Superdumb在NSDictionary中所有的數據轉換成NSString的,使他們可以寫入T他可以毫不費力地提交文件,如果你只是製作字典,然後嘗試立即保存,你就不會成功。

通過這種方式,我可以將所有CLLocation對象「序列化」爲一個字典,然後寫入一個plist文件。

S 0呂T I 0:N - 2

,我想出了這樣做的一個非常簡單(和更優雅)的方法:使用NSCoding。 因爲事實(我才發現)的CLLocation數據類型符合NSCoding,您可以通過的NSKeyedArchiver調用數據歸檔獲得一個blob描述你的陣列,然後將其直接存儲到plist中,這樣的:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"/Locations.plist"]; 
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

[data setValue:[NSKeyedArchiver archivedDataWithRootObject:arrayOfLocations] forKey:@"LocationList"]; 
[data writeToFile:path atomically:YES]; 
[data release]; 

and voila'。就那麼簡單! :)

基礎上,你可以很容易地回到你的數據相同的原則,通過NSKeyUnarchiver

self.arrayOfLocations = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData: (NSData *)[dict objectForKey:@"LocationList"]]]; 
3

KVC很容易。

這裏的NSObject的類的方法來獲取屬性名稱(要求<objc/runtime.h>

- (NSArray *)c_propertyNames { 
    Class class = [self class]; 
    u_int count = 0; 

    objc_property_t *properties = class_copyPropertyList(class, &count); 
    if (count <= 0) { 
     return nil; 
    } 
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, count)]; 

    NSMutableSet *retVal = [NSMutableSet setWithCapacity:count]; 
    [set enumerateIndexesWithOptions:NSEnumerationConcurrent 
          usingBlock:^(NSUInteger idx, BOOL *stop) { 
           const char *propName = property_getName(properties[idx]); 
           NSString *name = [NSString stringWithUTF8String:propName]; 
           [retVal addObject:name]; 
          }]; 
    return [retVal allObjects]; 
} 

然後使用它是這樣的:

NSArray *keys = [yourLocation c_propertyNames]; 
NSDictionary *dict = [yourLocation dictionaryWithValuesForKeys:keys]; 

然後保存該字典。

+0

類別代碼運行良好:它填充「鍵」NSArray,但只要我執行[dict writeToFile:path automatically:YES];沒有任何文件寫在文件上。如果你想,我用相對代碼更新了我的問題。謝謝! – holographix 2012-04-19 21:02:25

+0

好吧我已經做了更多的實驗,我發現如果我在NSDictionary中包含鍵「clientLocation」的元素,它將不會寫入plist文件,否則它就像魅力一樣。也許這是由於該字段是一個字符串有很多字符的事實引起的。這是該元素的內容:<00000000 ffff0000 94092907 20413b40 6a5c4eeb 797455c0 a6f71fc4 7fde7d40 00000000 00000000 00000000 0000f0bf 00000000 0000f0bf 00000000 0000f0bf 00000000 0000f0bf 00000000 0000f0bf 00000000 0000f0bf 00000000 0000f0bf bc043f09 c140b541 64000000 00000000 00000040 00000000> – holographix 2012-04-19 21:57:23

+0

好神祕解決了,我不得不通過循環解析數據。我會發布另一個解決方案,將我的代碼與您的代碼結合在一起。 – holographix 2012-04-19 22:08:15

3

我喜歡solution 2但如果所有的人試圖做的序列化可以更簡單的是寫直一份文件。

[NSKeyedArchiver archiveRootObject:arrayOfLocations toFile:path]; 
+0

確實不錯.. :)玩得很好。 – holographix 2013-02-20 16:06:09