开发者

Xcode: Show downloaded .txt content in label?

开发者 https://www.devze.com 2023-01-11 03:56 出处:网络
Probably really a rookie question here about xcode (for the iphone).. When I issue this command; NSString *externalData = [NSData dataWithContentsOfURL:[NSURL URLWithString: @\"http://blah.com/userl

Probably really a rookie question here about xcode (for the iphone)..

When I issue this command;

NSString *externalData = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://blah.com/userli开发者_开发技巧st.txt"]];

I can see it's download from my webserver. How can I make this 1 line show in a label?

I tried; label.text = externalData; [externalData release];

But this doesn't seem to work..it seems to crash the app in the simulator. Any ideas?


It crashes because +dataWithContentsOfURL: returns an NSData* which is not an NSString*. You want +stringWithContentsOfURL: instead. Note though, that this will block the main thread, which may not be desirable.

Edit:

To be clear, code like this:

NSString* foo = [NSString stringWithContentsOfURL:...];

Where you replace the appropriate sections of code with your own values.

0

精彩评论

暂无评论...
验证码 换一张
取 消