删除设备上保存的plist中的第一项

时间:2018-11-05 16:02:59

标签: ios objective-c nsarray plist nsmutabledictionary

抱歉,这有点长,但是我已经为此苦了一个星期……我通过修改此code创建了一个位置跟踪iOS应用程序。 LocationManager文件将新位置添加到我的“ LocationArray.plist”中,该位置保存在可以通过“文件共享”访问的设备上。

plist看起来像这样:LocationArray.plist

我制作了一个使用Web请求的方法,如果requestReply返回“ OK”,我想删除LocationArray中plist上的第一项(项目0)。因为它是保存的配置文件,所以如果我调用[self.myLocationArrayInPList removeObjectAtIndex:0],它会注册它已被删除,但实际上并没有从LocationArray.plist的savedProfile中删除它。

- (void)removeLocationFromPList {
     NSString *plistName = [NSString stringWithFormat:@"LocationArray.plist"];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *docDir = [paths objectAtIndex:0];
     NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docDir, plistName];

     NSMutableDictionary *savedProfile = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];
     for (NSDictionary *frame in _myLocationArrayInPlist) {
         NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
         NSNumber *two = [frame objectForKey:@"Latitude"];
         NSNumber *three = [frame objectForKey:@"Longitude"];
         NSDate *four = [frame objectForKey:@"Time"];
         NSArray *keys = [_myLocationDictInPlist allKeys];
         NSString *latitude = [two stringValue];
         NSString *longitude = [three stringValue];
         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
         [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
         NSString *time = [dateFormatter stringFromDate:four];

         NSURLComponents *components = [NSURLComponents componentsWithString:@"http://api.earlyconnect.com/Product/GPSLog.ashx"];
         NSURLQueryItem *identifier = [NSURLQueryItem queryItemWithName:@"id" value:uniqueIdentifier];
         NSURLQueryItem *lat = [NSURLQueryItem queryItemWithName:@"lat" value:latitude];
         NSURLQueryItem *lng = [NSURLQueryItem queryItemWithName:@"lng" value:longitude];
         NSURLQueryItem *tim = [NSURLQueryItem queryItemWithName:@"t" value:time];
         components.queryItems = @[ identifier, lat, lng, tim ];
         NSURL *url = components.URL; // URL with parameters filled in
         NSString *urlString = [url absoluteString];

         NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];

         NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
         [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
             NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
             NSData * responseData = [requestReply dataUsingEncoding:NSUTF8StringEncoding];
             NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
             if([requestReply isEqual:@"OK"])
             {
                // ERASE FIRST ITEM IN PLIST HERE
             }
         }] resume];
     }
 }

据我了解,我必须同时删除saveProfile中的myLocationArrayInPlist和myLocatiionDictInPlist ...,但我不确定。请帮忙!

2 个答案:

答案 0 :(得分:0)

从字典中删除第一项时,仅是从文件的内存副本中删除,实际上并没有更改文件本身。

删除完所有要删除的内容后,需要将字典保存回磁盘。

您可以使用:

[self.myLocationArrayInPList removeObjectAtIndex:0]
[self.myLocationArrayInPList writeToURL:URLtoFile error:&error]

在此处查看文档:{​​{3}}

答案 1 :(得分:0)

我添加了

            self.myLocationArrayInPlist = [savedProfile objectForKey:@"LocationArray"];
            [self->_myLocationArrayInPlist removeObject:frame];
            [savedProfile writeToFile:fullPath atomically:YES];

这也很好!