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 e.g.
let filename = (((outputFileUrl.absoluteString as NSString).stringByDeletingPathExtension as NSString).lastPathComponent ?? "video") + "-c"
but I recommend to use the URL related API
Related
As the title says, I've got a file downloaded from a server and I know the mimetype of it, but how can I work out what file extension should be used for it when writing to storage?
After hunting around on here, it was clear the answer involved Apple's Uniform Type Identifiers (UTI), but I couldn't find an example of exactly what I wanted to do. I'm sure this isn't perfect Swift code (I'm new to it as a language) because of how I'm handling the managed/unmanaged variables, but this does the conversion I need:
let unmanagedFileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, type as CFString, nil)?.takeRetainedValue()
let fileExtension = UTTypeCopyPreferredTagWithClass((unmanagedFileUTI)!, kUTTagClassFilenameExtension)?.takeRetainedValue()
I try to create a function for querying the Facebook database. Unfortunately, the syntax changed with the last version of swift. Maybe someone can post me the solution ?
Thx.
Func donneesFB
It's a lot easier to help you if you post your code as text instead of in an image.
Two things that will likely help you out here:
First, cast your result to the type of dictionary you are expecting before trying to access it:
guard let resultDict = result as? [String:Any] else { return }
You should now be able to use it like you tried to:
let nom = resultDict["name"] as? String
Secondly, for the error on your first line, simply get rid of the argument labels, nom, prenom and so on, leaving just the types.
I am trying to open a pdf from my Swift application but I cannot make it to work.
I know that there are a lot of questions related with this and in most of them they use this code:
UIApplication.shared.openURL(URL(string: "http://www.url.com" + id)!)
But I am getting the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
so I thought that the error was because the id was nil so I made two prints to see what was retrieved: one for url and one for id. Both of them are correct and if I copy the url on the browser navigation bar it works perfectly (I cannot provide the real url because it is of a client).
I have this function wrapped inside an #IBAction to detect when a button is clicked so I looked if the connection had been broken. It is also correct.
So I cannot understand why this error is happening. I spent some hours but cannot figure out what is causing this error.
Am I missing something? Should I codify the url in some way?
P.S: I am using Xcode8 and Swift3.
UPDATE: If I set www.google.es it is working perfectly. Can it be a problem using variables inside of the url?
Thanks in advance!
Finally, after some hours searching about the problem and following the recommendation of #rmaddy (Thanks!) I have split my function in three parts and I could see that the URL was returning nil value.
It was strange because I could copy the string into my browser navigation bar and it worked well so I thought that it could be something about encoding. One time I have encoded it I noticed that the id of the pdf was retrieved with a \r at the final of the id. Like this:
id004.pdf\r
The solution that I have done is the following:
var string = "http://www.url.com" + id
var index1 = string.index(string.endIndex, offsetBy: -1)
var substring1 = string.substring(to: index1)
let encodedString = substring1.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
UIApplication.shared.openURL(NSURL(string: encodedString!) as! URL)
Encoded string is necessary because if not the link no longer work.
Whenever I try to create an NSURL from it, I always receive nil back. How do I make it work?
Small example:
let str = "https://montr\\U00e9al.ca".stringByRemovingPercentEncoding!
NSURL(string: str)
Which always gives me nil back. Even if I replace the \u00e9 with é it still returns nil.
How should I be doing this?
You should make unicode in braces and add percent for it like this:
let str = "https://www.montr\u{00e9}al.ca".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
First, if you have any control over the server, you should fix the server to provide proper URLs. If you don't have control over it, but can choose a different service, do so. There's no guarantee that improperly encoded URLs will be the only problem coming from that server.
If you can't switch servers, you may need to do something like this:
let percentEncodedURLString = NSURL(dataRepresentation: str.dataUsingEncoding(NSUTF8StringEncoding)!, relativeToURL: nil).relativeString
This is the Swift version of what Apple recommends in the Foundation release notes. I strongly recommend that you read that section to understand the issues with other approaches.
Trying to start some Swift work. I am using
var imageData = UIImageJPEGRepresentation(image, compressionQuality:1.0)
but I get a warning "extraneous argument label 'compressionQuality' in call. I thought that in Swift the secondary parameters were either required or 'allowed' to be labelled, but this won't let me use it at all- fails building if I leave it. Since this is a system function, I can't use the # to require it. But I WOULD like to be able to name as many parameters as possible to make code more readable for myself, I like the ObjC method names, as verbose as they sometimes are.
Is there a way to set a compiler flag to allow extra argument labels?
You can't do like that, because that function doesn't declare any external parameter name. Internal parameter names can only be used within the function that declares them.
In Swift UIImageJPEGRepresentation method is declared as:
func UIImageJPEGRepresentation(_ image: UIImage!,
_ compressionQuality: CGFloat) -> NSData!
Check both parameters, both have internal name only so you can't use:
var imageData = UIImageJPEGRepresentation(image, compressionQuality:1.0)
Change that to:
var imageData = UIImageJPEGRepresentation(image,1.0)
Update Swift 4.2:
In swift 4.2, the above mentioned methods are no longer available. Instead of that you need to use:
// For JPEG
let imageData = image.jpegData(compressionQuality: 1.0)
// For PNG
let imageData = image.pngData()
Refer the API Document for more: Images & PDF
I had a similar problem, but Xcode was complaining about it in one of my funcions.
Turned out to be an extra } in my code, making the subsequent function declarations to be outside my class.
The error message was weird as hell, so I hope it hepls somebody else.