0

I have a data array showing a list of items with label information. On the media section, I want to insert a URL to run a video. What would be a correct procedure to load a url (using NSURL) after the @"Play Video" string? Then when the button is pressed, the url would load and display the video. THe URL is different for each condition. Thanks for any assistance!!

- (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];
Monolo
  • 18,205
  • 17
  • 69
  • 103
Chip Russell
  • 65
  • 10

2 Answers2

0

You mean how to include the NSURL as a second item in your NSArray media?

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

See https://stackoverflow.com/a/1981508/436641 for some caveats and sample code on how one might create an NSURL object from a URL string.

If you already have an NSURL object and want to just add the string representation of that object rather than the NSURL object itself:

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

Once you have your media array, the "Play Video" string can be retrieved as [media objectAtIndex:0] and the URL can be retrieved as [media objectAtIndex:1].

From an OO perspective, perhaps ideally, you wouldn't use an array here but would rather create a Media class for your program to instantiate. But the above answers the question (how to add an NSURL to an array).

Community
  • 1
  • 1
Trott
  • 66,479
  • 23
  • 173
  • 212
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];
Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • Hi, Here is what I have done, it compiles but dumps when I try to call it. I added the following.... Play *play; NSArray *media; NSURL *url_; play = [[Play alloc] init]; play.title = @"Introduction"; play.part = @"Getting in the Game"; url_ = [NSURL URLWithString:@"http://abc.com/video.mp4"]; media = [[NSArray alloc] initWithObjects:@"Play Video",url_, nil]; play.media = media; [playList addObject:play]; This compiles with no errors but gets a screen dump. Any suggestions? – Chip Russell Dec 28 '11 at 05:04
  • 2011-12-28 10:19:08.411 [2069:707] -[NSURL isEqualToString:]: unrecognized selector sent to instance 0x353550 2011-12-28 10:19:08.415 [2069:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL isEqualToString:]: unrecognized selector sent to instance 0x353550' *** First throw call stack: (0x382df8bf 0x37e2b1e5 0x382e2acb 0x382e1945 0x3823c680 0x31d8b20d 0x518d 0x31de79cb 0x31de6aa9 0x31de6233 0x31d8ad29 0x3823e22b 0x378d8381 0x378d7f99 0x378dc11b 0x378dbe57 0x378d3d85 0x382b3b4b 0x382b1d87 0x382b20e1 0x382354dd... – Chip Russell Dec 28 '11 at 15:22
  • how can you compare two different objects like that? you should use [[url absoluteString] isEqualToString:@"http://whatever.com"] wherever you are trying to compare url with a string in your code, – Saurabh Passolia Dec 29 '11 at 07:10