使用URL传递参数

时间:2018-06-13 06:41:50

标签: objective-c

-(void)getWord:(NSString*)upperCaseString :(NSString *)combinedString :(NSString *)wordIndex
{
    NSURL *url = [NSURL URLWithString:@"http://quicklanguages.com/materiales/quicklanguages/audios/%@/%@-%@.mp3",@"upperCaseString", @"combinedString", @"wordIndex" ];
    //  [tweetSheet setInitialText:[NSString stringWithFormat:@"%@", 
    [[_items objectAtIndex:indexPath.row] objectForKey:@"redirect_url"]]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; 
    // Now we are assigning it in an instance variable thus ARC will not deallocate it.
    [audioPlayer play];
}

我创建一个方法并传递三个参数,现在可以用这个URL设置这些参数.....

1 个答案:

答案 0 :(得分:0)

正确的方法是:

NSString *urlString = [NSString stringWithFormat:@"http://bla.bla/bla/%@/%@-%@.mp3", @"upperCaseString", @"combinedString", @"wordIndex"];
NSURL *url = [NSURL URLWithString:urlString];

修改

最终代码如下所示:

-(void)getWord:(NSString*)upperCaseString :(NSString *)combinedString :(NSString *)wordIndex
{
    NSString *urlString = [NSString stringWithFormat:@"http://quicklanguages.com/materiales/quicklanguages/audios/%@/%@-%@.mp3",@"upperCaseString", @"combinedString", @"wordIndex"];
    NSURL *url = [NSURL URLWithString:urlString];
    //  [tweetSheet setInitialText:[NSString stringWithFormat:@"%@", 
    [[_items objectAtIndex:indexPath.row] objectForKey:@"redirect_url"]]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil]; 
    // Now we are assigning it in an instance variable thus ARC will not deallocate it.
    [audioPlayer play];
}