Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

There is no Swift Reference for the NSURL class

Would anybody mind explaining the syntax to me from the NSURL Class reference in swift https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/index.html

I am attempting to transfer this project to swift https://robots.thoughtbot.com/streaming-audio-to-multiple-listeners-via-ios-multipeer-connectivity and I need some help with this section

NSURL *url = [myMediaItem valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:asset error:nil];
AVAssetReaderTrackOutput *assetOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:asset.tracks[0] outputSettings:nil];

[self.assetReader addOutput:self.assetOutput];
[self.assetReader startReading];

What would the swift equivalent of this snippet be?

1 Answer

  1. You're looking at the Mac Developer Library. Here's the equivalent for iOS.

  2. If you look at the top right-ish center of the page, you should be able to see where you can select which language you want to view it in. Your choices are Objective-C, Swift, or both.

  3. Here's the equivalent code in Swift:

let url = myMediaItem.valueForProperty(MPMediaItemPropertyAssetURL)
let asset = AVURLAsset.URLAssetWithURL(url, options: nil)
let assetReader = AVAssetReader.assetReaderWithAsset(asset, error: nil)
let assetOutput = AVAssetReaderTrackOutput.assetReaderTrackOutputWithTrack(asset.tracks[0], outputSettings: nil)

self.assetReader.addOutput(self.assetOutput)
self.assetReader.startReading()

I wrote that code off the top of my head without testing it, so I can't guarantee it'll work (though I think it will). If it doesn't, it'll be very close, and will only require a couple quick syntax changes that Xcode should be able to fix for you (but I think it'll work)

So I am getting errors everywhere that just don see to make sense, such as view controller.type does not have a member called url, asset, assetreader, and assetoutput, how do I solve this, as they are obviously being declared right there.

Could you post all the code in the relevant file here?

Nevermind I fixed the error, I was attempting to access the variables within the class and not a specific function, however I am receiving this error Cannot invoke 'URLAssetWithURL' with an argument list of type '(Anyobject!, options: nil)' in this spot

    var asset = AVURLAsset().URLAssetWithURL(url, options: nil)