Objective-C NSURL URLWithString在使用变量时似乎失败

Mar*_* D. 2 iphone cocoa cocoa-touch objective-c

所以这是我在StackOverflow上的第一篇文章,请原谅,如果我的请求编写得不好.

我试图使用以下行将类型为NSString的变量加载到NSURL中:

audioPlayer = [[AudioPlayer alloc] initPlayerWithURL:[NSURL URLWithString:aArchiveItem.streamURL] delegate:self];
Run Code Online (Sandbox Code Playgroud)

在我使用的AudioPlayer实现中:

-(id)initPlayerWithURL:(NSURL *)url delegate:(id<AudioPlayerDelegate>) aDelegate {
    self = [super init];

    delegate = aDelegate;

    queue = [[AudioQueue alloc] initQueueWithDelegate:self];

    fileStream = [[AudioFileStream alloc] initFileStreamWithDelegate:self];
    [fileStream open];

    request = [[AudioRequest alloc] initRequestWithURL:url delegate:self];

    return self;
}
Run Code Online (Sandbox Code Playgroud)

当我硬编码URL字符串(如URLWithString:@" http://www.example.com ")的调用audioPlayer工作完美,但试图与aArchiveItem.streamURL的失败值来填充它的时候...

我在这做错了什么?

小智 5

可能你的网址有空白区域尝试在使用网址之前执行以下操作:

aArchiveItem.streamURL=[aArchiveItem.streamURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Run Code Online (Sandbox Code Playgroud)