2014-02-17 38 views
0

我从我的AWS服务器上拉取歌曲名称数组。NSString作为参数失败,但文字字符串的作品?

我的下一步是使用这些歌曲名称之一作为检索其流传输URL的请求中的参数。

//[1] Initialize the S3 Client. 
    self.s3 = [[AmazonS3Client alloc] initWithAccessKey:@"blah" withSecretKey:@"blah"]; 
    self.s3.endpoint = [AmazonEndpoints s3Endpoint:US_WEST_2]; 



    //[2] Get an array of song names 
    NSArray *song_array = [self.s3 listObjectsInBucket:@"blahblah"]; 
    NSLog(@"the objects are %@", song_array); 


    //[3] Get a single song name from the array 
    NSString *song1 = [[NSString alloc] init]; 
    song1 = (NSString *)[song_array objectAtIndex:1]; 
    NSLog(@"%@", song1); 

    NSString * song2 = @"Rap God.mp3"; 
    NSLog(@"%@", song2); 


    //[4] Get the Song URL 
    S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init]; 
    gpsur.key      = song2; 
    gpsur.bucket     [email protected]"soundshark"; 
    gpsur.expires     = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; 
    NSError *error; 
    NSURL *url = [self.s3 getPreSignedURL:gpsur error:&error]; 
    NSLog(@"the url is %@", url); 

Song2完美地作为参数gpsur.key。

然而,如果我使用松1作为参数,它失败,错误

终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“ - [S3ObjectSummary stringWithURLEncoding]:无法识别的选择发送到实例0x175aef30

当我使用的NSLog,既松1和song2打印完全相同的字符串“说唱God.mp3”

为什么出错?为什么我不能使用数组中的字符串?它具有完全相同的价值?

回答

1

变化

NSString *song1 = [[NSString alloc] init]; 
song1 = (NSString *)[song_array objectAtIndex:1]; 
NSLog(@"%@", song1); 

S3ObjectSummary *s3object = [song_array objectAtIndex:1]; 
NSString *song1 = [s3object description]; 
NSLog(@"%@", song1); 

如果它的工作将得到更好的改变

NSString *song1 = [s3object description]; 

NSString *song1 = [s3object etag]; 

NSString *song1 = [s3object key]; 

我不熟悉S3ObjectSummary,所以我不能建议什么变化是更好的。

+0

我爱你...... – user1161310

1

问题是“song1”实际上不是NSString。以下意思是说你试图在不存在的类S3SObjectSummary的对象上调用一个方法。这告诉你“song1”是一个S3SObjectSummary而不是NSString。

'-[S3ObjectSummary stringWithURLEncoding]: unrecognized selector sent to instance 

要解决这个问题,我发现其中介绍了如何从该对象与属性“说明”获得的NSString值S3ObjectSummary的文档。 [S3ObjectSummary说明]

http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/S3ObjectSummary.html#//api/name/description

所以你的情况NSString的是song1.description

把这一切在一起你会得到如下。编码目的

入住此link

//Grab the S3ObjectSummary from the array 
    S3ObjectSummary *song1 = (S3ObjectSummary*)[song_array objectAtIndex:1]; 
    NSLog(@"%@", song1); 

// Use the description property of S3ObjectSummary to get the string value. 
    NSString *stringFromObjectSummary = song1.description; 


    S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init]; 
    gpsur.key      = stringFromObjectSummary; 
0

乍一看,你应该使用stringByAddingPercentEscapesUsingEncoding到不允许的字符在URL编码。

此外,你应该这样尝试从数组元素构造一个字符串。

NSString *song1 = [NString stringWithFormat:@"%@", [song_array objectAtIndex:1]]; 
NSLog(@"%@", song1);