首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableArray writeToUrl

NSMutableArray writeToUrl
EN

Stack Overflow用户
提问于 2009-06-20 22:30:59
回答 2查看 2.3K关注 0票数 0

是否可以使用:

代码语言:javascript
复制
[NSMutableArray writeToURL:(NSString *)path atomically:(BOOL)AuxSomething];

为了将文件(NSMutableArray) XML文件发送到url,并更新url以包含该文件?

例如:我有一个数组,我想把它上传到一个特定的URL,下一次应用启动时,我想下载那个数组。

代码语言:javascript
复制
NSMutableArray *arrayToWrite = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",nil];

[arrayToWrite writeToURL:

[NSURL urlWithString:@"mywebsite.atwebpages.com/myArray.plist"] atomically:YES]; 

在运行时:

代码语言:javascript
复制
NSMutableArray *arrayToRead = 

[[NSMutableArray alloc] initWithContentsOfURL:[NSURL           urlWithString:@"mywebsite.atwebpages.com/myArray.plist"]];

这意味着,我想写一个网址,这是在一个虚拟主机服务(例如batcave.net,网址接收信息,并相应地更新服务器端文件)。像设置这样的高分,用户发送他的分数,服务器更新它的文件,其他用户在运行时下载高分。

EN

回答 2

Stack Overflow用户

发布于 2009-06-21 00:47:33

至于问题的第一部分,我假设您想要使用NSMutableArray的内容来形成某种类型的URL请求(例如,您将发送到web服务并期望返回一些信息的POST……

没有预先构建的方法可以将NSMutableArray的内容发送到URL,但有一些简单的方法可以自己完成。例如,您可以遍历数组的数据,并利用NSURLRequest创建符合web服务接口的URL请求。一旦构造了请求,就可以通过传递一个NSURLConnection对象来发送它。

考虑这个使用Obj-C数组提供数据的客户端代码的简单而不完整的示例……

代码语言:javascript
复制
NSMutableData *dataReceived; // Assume exists and is initialized
NSURLConnection *myConnection;

- (void)startRequest{
    NSLog(@"Start");

    NSString *baseURLAddress = @"http://en.wikipedia.org/wiki/";

    // This is the array we'll use to help make the URL request
    NSArray *names = [NSArray arrayWithObjects: @"Jonny_Appleseed",nil];
    NSString *completeURLAsString = [baseURLAddress stringByAppendingString: [names objectAtIndex:0]];

    //NSURLRequest needs a NSURL Object
    NSURL *completeURL = [NSURL URLWithString: completeURLAsString];

    NSURLRequest *myURLRequest = [NSURLRequest requestWithURL: completeURL];

    // self is the delegate, this means that this object will hanlde
    // call-backs as the data transmission from the web server progresses
    myConnection = [[NSURLConnection alloc] initWithRequest:myURLRequest delegate: self startImmediately:YES];
}

// This is called automatically when there is new data from the web server,
// we collect the server response and save it
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"Got some");
    [dataReceived appendData: data];
}

// This is called automatically when transmission of data is complete
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // You now have whatever the server sent...
}

为了回答问题的第二部分,web请求的接收者可能需要一些脚本或基础设施来做出有用的响应。

票数 1
EN

Stack Overflow用户

发布于 2009-06-21 05:46:25

在这里,回答这个问题:

Creating a highscore like system, iPhone side

我不能编辑我的帖子,因为我是以匿名用户的身份从我的iPhone发帖的,对不起。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1022714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档