无法将 NSArray 写入文件

发布于 2024-12-10 11:27:30 字数 1192 浏览 0 评论 0原文

我正在使用 google contact data Objective C API 来获取联系人。我从谷歌服务器获取了联系人数组,现在我想将联系人写入文件。我正在使用 writeToFile:atomically: 方法将数组写入文件,但此方法对我不起作用,因为我觉得 gdata API 的输出数组不包含属性列表对象。请建议任何替代解决方案。

-(void)fetchData{
    GDataServiceGoogleContact *service=[[GDataServiceGoogleContact alloc] init];
    [service setShouldCacheResponseData:YES];
    [service setServiceShouldFollowNextLinks:YES];
   [service setUserCredentialsWithUsername:[mUsername stringValue] password:[mPassword stringValue]];

    // GENERATING THE URL
    NSURL *feedURL=[GDataServiceGoogleContact contactFeedURLForUserID:kGDataServiceDefaultUser];
    GDataQuery *contQuery=[GDataQueryContact contactQueryWithFeedURL:feedURL];
    [contQuery setShouldShowDeleted:YES];
    [contQuery setMaxResults:2000];
    GDataServiceTicket *ticket=[service fetchFeedWithQuery:contQuery delegate:self didFinishSelector:@selector(hasFetchedContacts:feed:error:)];    
}


-(void) hasFetchedContacts:(GDataServiceTicket*) ticket  feed:(GDataFeedContact*) contacts error:(NSError*) err
{
    NSArray *contactList=[contacts entries];
    NSLog(@"%d",[list writeToFile:@"/Users/subhranil/Desktop/contactList" atomically:NO]);  

}

I am using google contact data objective c APIs for fetching contacts. I got contacts array from google server now i want to write contact to file. i am using writeToFile:atomically: method for writing array to file but This method is not working for me since i feel that output array from gdata API not contain property list objects. Please suggest any alternate solution.

-(void)fetchData{
    GDataServiceGoogleContact *service=[[GDataServiceGoogleContact alloc] init];
    [service setShouldCacheResponseData:YES];
    [service setServiceShouldFollowNextLinks:YES];
   [service setUserCredentialsWithUsername:[mUsername stringValue] password:[mPassword stringValue]];

    // GENERATING THE URL
    NSURL *feedURL=[GDataServiceGoogleContact contactFeedURLForUserID:kGDataServiceDefaultUser];
    GDataQuery *contQuery=[GDataQueryContact contactQueryWithFeedURL:feedURL];
    [contQuery setShouldShowDeleted:YES];
    [contQuery setMaxResults:2000];
    GDataServiceTicket *ticket=[service fetchFeedWithQuery:contQuery delegate:self didFinishSelector:@selector(hasFetchedContacts:feed:error:)];    
}


-(void) hasFetchedContacts:(GDataServiceTicket*) ticket  feed:(GDataFeedContact*) contacts error:(NSError*) err
{
    NSArray *contactList=[contacts entries];
    NSLog(@"%d",[list writeToFile:@"/Users/subhranil/Desktop/contactList" atomically:NO]);  

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心未许 2024-12-17 11:27:30

将其包装到 NSData 中:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:contactList];

然后将 NSData 保存到文件中:

[data writeToFile:@"/Users/subhranil/Desktop/contactList" atomically:NO];

您稍后可以使用以下方法将数据恢复回 NSArray:

NSData *data = [NSData dataWithContentsOfFile: @"yourFilePath"];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data]

只需 确保 NSArray 中的对象符合 <代码>NSCoding。

Wrap it up to NSData with:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:contactList];

Then save NSData to file with:

[data writeToFile:@"/Users/subhranil/Desktop/contactList" atomically:NO];

You can later restore the data back to NSArray using:

NSData *data = [NSData dataWithContentsOfFile: @"yourFilePath"];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data]

Just make sure that objects inside your NSArray conform to NSCoding.

梦回梦里 2024-12-17 11:27:30

您可以使用 byte 数组来实现此目的,并使用 NSData 来写入文件。
用于保存:

NSData *data=[[NSData alloc] initWithBytes:[contacts entries] length:total];
[data writeToFile:@"path" atomically:YES];

total= 数组的总大小(以字节为单位)

用于检索:

   NSData *newdata = [NSData dataWithContentsOfFile:@"path"];
   NSUInteger len = [newdata length];
   Byte *byteData = (Byte*)malloc(len);
   memcpy(byteData, [newdata bytes], len);

byteData 现在将包含 GDataEntryContact 对象的数组,您可以相应地使用它们。

You can use byte array for this purpose and NSData for writing to file.
For saving:

NSData *data=[[NSData alloc] initWithBytes:[contacts entries] length:total];
[data writeToFile:@"path" atomically:YES];

total= The total size of the array in bytes

For retrieving:

   NSData *newdata = [NSData dataWithContentsOfFile:@"path"];
   NSUInteger len = [newdata length];
   Byte *byteData = (Byte*)malloc(len);
   memcpy(byteData, [newdata bytes], len);

byteData will now contain an array of GDataEntryContact objects and you can use them accordingly.

心凉怎暖 2024-12-17 11:27:30

您可以使用 xml 作为生成器对 GDataObject 进行编码/解码。

编码:

[entry setNamespaces:[entry completeNamespaces]];
NSString *xml = [[entry XMLElement] XMLString];
if (nil != xml) 
{
    //Store your xml NSString to a file
}

解码:

NSString *xml = //Read your XML String from file;
NSXMLElement *xmlElement = [[NSXMLElement alloc] initWithXMLString:xml error: &error];
if (!error) {
    return [[GDataEntryDocBase alloc] initWithXMLElement:xmlElement parent: nil];
}

You can encode/decode GDataObject using an xml as generator.

Encode:

[entry setNamespaces:[entry completeNamespaces]];
NSString *xml = [[entry XMLElement] XMLString];
if (nil != xml) 
{
    //Store your xml NSString to a file
}

Decode:

NSString *xml = //Read your XML String from file;
NSXMLElement *xmlElement = [[NSXMLElement alloc] initWithXMLString:xml error: &error];
if (!error) {
    return [[GDataEntryDocBase alloc] initWithXMLElement:xmlElement parent: nil];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文