无法将 NSArray 写入文件
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其包装到 NSData 中:
然后将 NSData 保存到文件中:
您稍后可以使用以下方法将数据恢复回 NSArray:
只需 确保
NSArray
中的对象符合 <代码>NSCoding。Wrap it up to NSData with:
Then save NSData to file with:
You can later restore the data back to NSArray using:
Just make sure that objects inside your
NSArray
conform toNSCoding
.您可以使用
byte
数组来实现此目的,并使用NSData
来写入文件。用于保存:
total= 数组的总大小(以字节为单位)
用于检索:
byteData
现在将包含 GDataEntryContact 对象的数组,您可以相应地使用它们。You can use
byte
array for this purpose andNSData
for writing to file.For saving:
total= The total size of the array in bytes
For retrieving:
byteData
will now contain an array of GDataEntryContact objects and you can use them accordingly.您可以使用 xml 作为生成器对 GDataObject 进行编码/解码。
编码:
解码:
You can encode/decode GDataObject using an xml as generator.
Encode:
Decode: