需要帮助才能将NSURL字符串添加到Array

时间:2011-12-23 14:18:20

标签: iphone nsarray nsurl

我有一个数据数组,显示带有标签信息的项目列表。在媒体部分,我想插入一个URL来运行视频。在@“播放视频”字符串后加载网址(使用NSURL)的正确程序是什么?然后,当按下按钮时,URL将加载并显示视频。每个条件的URL都不同。谢谢你的帮助!!

- (void)createData {

NSMutableArray *playList = [[NSMutableArray alloc] init];
Play *play;
NSArray *media;

play = [[Play alloc] init];
play.title = @"Introduction";
play.part = @"Getting in the Game";
media = [[NSArray alloc] initWithObjects:@"Play Video", nil];
play.media = media;
[playList addObject:play];

play = [[Play alloc] init];
play.title = @"Rule #1";
play.part = @"Getting in the Game";
media = [[NSArray alloc] initWithObjects:@"Play Video", nil];
play.media = media;
[playList addObject:play];

play = [[Play alloc] init];
play.title = @"Rule #2";
play.part = @"Getting in the Game";
media = [[NSArray alloc] initWithObjects:@"Play Video", nil];
play.media = media;
[playList addObject:play];

2 个答案:

答案 0 :(得分:0)

您的意思是如何将NSURL作为NSArray media中的第二项包含在内?

media = [[NSArray alloc] initWithObjects:@"Play Video", myNsUrl, nil];

有关如何从URL字符串创建NSURL对象的一些警告和示例代码,请参阅https://stackoverflow.com/a/1981508/436641

如果您已经有一个NSURL对象,并且只想添加该对象的字符串表示而不是NSURL对象本身:

media = [[NSArray alloc] initWithObjects:@"Play Video", [myNsUrl absoluteString], nil];

获得media数组后,可以将“播放视频”字符串检索为[media objectAtIndex:0],并将该网址检索为[media objectAtIndex:1]

从OO的角度来看,也许理想情况下,你不会在这里使用数组,而是为你的程序创建一个Media类来实例化。但上面回答了问题(如何将NSURL添加到数组中)。

答案 1 :(得分:0)

Play *play;
NSArray *media;
NSURL *url;

play = [[Play alloc] init];
play.title = @"Introduction";
play.part = @"Getting in the Game";
//add this line
url = [NSURL urlWithString:@"http://abc.com/video.mp4"];
media = [[NSArray alloc] initWithObjects:@"Play Video",url, nil];
play.media = media;
[playList addObject:play];