获取网页文本到 Label
Get web text to Label
我需要从 URL http://cast.midiaip.com.br:6530/currentsong?sid=1 获取文本并将其显示在 UILabel 中
我的代码是这样的:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *musicasLabel;
- (IBAction)textoButton:(id)sender;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize musicasLabel;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)textoButton:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://cast.midiaip.com.br:6530/currentsong?sid=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *string = [NSString stringWithUTF8String:[data bytes]];
musicasLabel.text = string;
}
@end
我总是得到错误:
2015-04-17 02:55:21.637 teste label[14194:797672] ***
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '***
+[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cf1bf35
__exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010cbb4bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010cf1be6d +[NSException raise:format:] + 205
3 Foundation 0x000000010c719464 +[NSString stringWithUTF8String:] + 78
4 teste label 0x000000010c6885e5 -[ViewController textoButton:] + 181
5 UIKit 0x000000010d3098be -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010d410410 -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x000000010d40f7df -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x000000010d34f308 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x000000010d34fc33 -[UIWindow sendEvent:] + 683
10 UIKit 0x000000010d31c9b1 -[UIApplication sendEvent:] + 246
11 UIKit 0x000000010d329a7d _UIApplicationHandleEventFromQueueEvent + 17370
12 UIKit 0x000000010d305103 _UIApplicationHandleEventQueue + 1961
13 CoreFoundation 0x000000010ce51551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010ce4741d __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x000000010ce46a54 __CFRunLoopRun + 868
16 CoreFoundation 0x000000010ce46486 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x00000001104ea9f0 GSEventRunModal + 161
18 UIKit 0x000000010d308420 UIApplicationMain + 1282
19 teste label 0x000000010c688ae3 main + 115
20 libdyld.dylib 0x000000010f4ab145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
非常感谢!!!
您没有正确地初始化您的字符串。您的问题在这里得到解答:
convert UTF-8 encoded NSData to a NSString
我需要从 URL http://cast.midiaip.com.br:6530/currentsong?sid=1 获取文本并将其显示在 UILabel 中 我的代码是这样的: ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *musicasLabel;
- (IBAction)textoButton:(id)sender;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize musicasLabel;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)textoButton:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://cast.midiaip.com.br:6530/currentsong?sid=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *string = [NSString stringWithUTF8String:[data bytes]];
musicasLabel.text = string;
}
@end
我总是得到错误:
2015-04-17 02:55:21.637 teste label[14194:797672] ***
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '***
+[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cf1bf35
__exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010cbb4bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010cf1be6d +[NSException raise:format:] + 205
3 Foundation 0x000000010c719464 +[NSString stringWithUTF8String:] + 78
4 teste label 0x000000010c6885e5 -[ViewController textoButton:] + 181
5 UIKit 0x000000010d3098be -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010d410410 -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x000000010d40f7df -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x000000010d34f308 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x000000010d34fc33 -[UIWindow sendEvent:] + 683
10 UIKit 0x000000010d31c9b1 -[UIApplication sendEvent:] + 246
11 UIKit 0x000000010d329a7d _UIApplicationHandleEventFromQueueEvent + 17370
12 UIKit 0x000000010d305103 _UIApplicationHandleEventQueue + 1961
13 CoreFoundation 0x000000010ce51551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010ce4741d __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x000000010ce46a54 __CFRunLoopRun + 868
16 CoreFoundation 0x000000010ce46486 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x00000001104ea9f0 GSEventRunModal + 161
18 UIKit 0x000000010d308420 UIApplicationMain + 1282
19 teste label 0x000000010c688ae3 main + 115
20 libdyld.dylib 0x000000010f4ab145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
非常感谢!!!
您没有正确地初始化您的字符串。您的问题在这里得到解答: convert UTF-8 encoded NSData to a NSString