简体   繁体   中英

Deprecated methods in xcode7/swift 2.0

I am in the middle of updating my app which was built in ios 8 to ios9 by using xcode7/swift 2.0. Like many other people, my app crashed very heavily. The basic concept of my app is to let users to upload videos/images to a certain event. Right now, I am having an error message saying

object not found for update (Code: 101, Version: 1.9.0)

from parse. I thought it was Parse's error at first but figured out I may have made some dumb mistakes while I was updating codes. If you can take a look at the changes I made and point out the errors I made, it would be super helpful.

I changed following:

var outputURL = NSURL.fileURLWithPath(NSTemporaryDirectory().stringByAppendingPathComponent("\(filename)").stringByAppendingString(".mp4"))

to

var outputURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("\(filename).mp4")

I made this change because stringByAppendingString is deprecated now.

Other change i made is:

let filename = (outputFileUrl.absoluteString.stringByDeletingPathExtension.lastPathComponent ?? "video") + "-c"

to

let filename = (outputFileUrl.URLByDeletingPathExtension?.lastPathComponent ?? "video") + "-c"

I am pretty sure I did right with converting functions. It would be greatly appreciated if anyone can point out what I did wrong in those two changes.

The difference of the first example is:

  • The former syntax creates an NSString object
  • The new syntax creates an NSURL object

Either use the URL related API for example instead of …contentsOfFile use …contentsOfURL or get the path of the URL from the path property

let outputURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("\(filename).mp4").path!

The second example returns a string with percent encoded special characters in the former syntax and a string without percent encoding in the new syntax. You might add the percent encoding.

An alternative method to keep the older syntax is to cast the String to NSString eg

let filename = (((outputFileUrl.absoluteString as NSString).stringByDeletingPathExtension as NSString).lastPathComponent ?? "video") + "-c"

but I recommend to use the URL related API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM