【问题标题】:Get web text to Label获取 Web 文本到标签
【发布时间】:2015-04-17 06:12:39
【问题描述】:

我需要从 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) 

非常感谢!!!

【问题讨论】:

    标签: objective-c utf-8 nsstring uilabel sigabrt


    【解决方案1】:

    您的字符串初始化不正确。您的问题在这里得到解答: convert UTF-8 encoded NSData to a NSString

    【讨论】:

    • 谢谢你的回答,但这没有用,标签是空白的。我的代码: - (IBAction)textoButton:(id)sender { NSURL *url = [NSURL URLWithString:@"cast.midiaip.com.br:6530/currentsong?sid=1"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSString *string = [[NSString alloc] initWithData:数据编码:NSUTF8StringEncoding]; musicasLabel.text = string; NSLog(@" Data %@, string %@", data, string); 日志:2015-04-17 04:27:54.091 teste label[15141:855436]数据(空),字符串
    • 此代码:NSURL *url = [NSURL URLWithString:@"http://cast.midiaip.com.br:6530/currentsong?sid=1"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 为我工作。我得到了正确的 NSString 值。
    • 对我不起作用,我的标签为空白,我使用 nslog,数据为空,字符串为 blanc。我做错了什么?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多