Today i updated my xcode to version 7 with swift 2.0.
Then i got so many errors in my production app, But i already fixed most of it by myself.
The problem is some of it i don't know how to fix.
So the images below are errors that i could not fix it myself.
If anyone knows how to fix it please help.
Thanks!
As explain in the comment, you simply have to rewrite those lines by checking the method signature :
1.
// Use the NSURL methods instead of String ones
let path = NSURL(fileURLWithPath: documentsFolder).URLByAppendingPathComponent("baseDeck.sqlite").path!
2.
// Make sure the productId is a String
request = SKProductsRequest(productIdentifiers: [productId])
3.
// Be careful to parameters, they are optionals
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) { (data: CMAccelerometerData?, error: NSError?) -> Void in
//...
}
4.
// Be careful too, the invalidProductIdentifiers method return an array of string whereas the response.products an array of SKProduct
let products = responses.products.filter { productIndentifiers.contains($0.productIdentifier) }
I hope it'll help you.
Related
Im using Xcode 8.3 with Swift 3. I have written one method named pdfFromData(data:) to form the pdf document from the Data, whenever I build my project its not getting build due to this method, means the compiler is got stopped/hanged when it start compile particular file where I coded pdfFromData(data:) method(In Xcode 8.2 with Swift 3 it worked fine). Whenever i comment this method and build means everything working fine.
func pdfFromData(data: Data) -> CGPDFDocument? { // Form pdf document from the data.
if let pdfData = data as? CFData {
if let provider = CGDataProvider(data: pdfData) {
let pdfDocument = CGPDFDocument(provider)
return pdfDocument
}
}
return nil
}
What's wrong with this method?. I want to build my project with this method as well. Thanks in advance.
I tried debugging your issue. This is what I found out:
if let pdfData = data as? CFData {
}
The above line for casting object of type Data to CFData is where it's taking too much time to build.
Replacing that with the following piece of code significantly reduces your build time.
let pdfNsData: NSData = NSData(data: data) // convert `Data` to `NSData`
if let cfPdfData: CFData = pdfNsData as? CFData {
// cast `NSData` to `CFData`
}
NSData and CFData are toll-free bridged.
Please let me know if there's any doubt
I'm running into a compiler error when using the following code:
func saveImageToDisk() {
let imageData = UIImagePNGRepresentation(imageView.image!)!
let fileName = getDocumentsDirectory().appendingPathComponent("image.png")
imageData.writeToFile(fileName, atomically: true)
}
The error is: Value of type 'Data' has no member 'writeToFile'
Could this be a compiler error, or something I'm missing? Thanks
SE-0005 proposed a better translation of Objective-C APIs into Swift and that affected NSData (or just Data now). Instead of writeToFile you'll have to use write(to:options:) (or even just write(to:)). Here is the documentation for the updated method.
After updating to the latest Parse SDK 1.8.5, I am receiving two errors surrounding the findObjectsInBackgroundWithBlock function. Two errors return on the same line:
if let objects = query.findObjects() as? [PFObject]
I have tried changing it to as [PFObject]? with no luck. The errors are as follows:
Call can throw, but it is not marked with 'try' and the error is not handled
AND Conditional cast from '[PFObject]' to '[PFObject]' always succeeds
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = query.findObjects() as? [PFObject] {
for object in objects {
object.deleteInBackgroundWithBlock{ (success, error) -> Void in
if (success) {
print("Worked")
print(objects.count)
self.viewDidAppear(true)
} else {
}
}
I have researched similar problems, and tried to fix it to query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in but this has done nothing either.
I am not sure how to fix either of these errors, and am very confused as to why they suddenly popped up. All of my functions were running perfectly, and have been for weeks, and suddenly these errors have popped up.
changing it from:
if let objects = query.findObjects() as? [PFObject]
to
if let objects = objects as [PFObject]?
seemed to fix the problem, but I am not sure if it is now going to run properly.
For other lines with the same error, XCode suggested changing it to:
self.rooms = (results as [PFObject]?)!
and that took away the error. I am pretty new to Swift coding, so I am not sure exactly what has happened, if anyone has any insight?
Using findObjects() is much more difficult with the new update. This is because with Xcode 7 and Swift 2, Swift became better at handling errors and Parse took advantage of these new methods to allow findObjects() to throw an error. It would be your job to handle the error and provide other code to run in case of such an error. In addition to these new requirements, findObjects() runs synchronously and will slow down your app. You should be using findObjectsInBackground() which runs asynchronously. This will solve both of your problems. If you need hep implementing this function, there is a lot of documentation and question/answers online.
Removing as [PFObject]? from if let objects = objects as [PFObject]? should solve your conditional cast error.
I'm getting this error in latest version of xcode using swift 2
on line
let s = linkTxt.text
Text in linkTxt appears by button "pasteFromClipBoard"
let s = linkTxt.text
let u = NSURL(string: s!)
let file = u?.lastPathComponent
What is the reason of it and how to fix it?
Update:
the problem appears in function saveData() which calls when file downloading is finished. It calls from NSURLSessionDataTask function. More interesting, that in start-downloading-button there are the same lines where filename is generating and there is no such error on it. I fixed these issues by declaring variables, writing text's values into them and use these variables in saveData() except textObject.text; I had to delete lines with NSUserDefaults from saveData() too because I got the same error. Did understand nothing >_<
Update 2:
It's really a bug. I've deleted this line and wrote again - problem fixed
linkTxt.txt is returning nil and NSURL(string: s!) will try to forcefully unwrap it.
let s = linkTxt.text
if let s = linkTxt.txt {
let u = NSURL(string: s!)
let file = u?.lastPathComponent
}
Hi I am really new to coding in Swift, and am trying to follow the codes in this book: http://www.apress.com/9781484202098. Learn iOS 8 App Development 2nd Edition by James Bucanek
In particular, I am working through Chapter 3 - building a URL shortening app, but despite having copied the code exactly, I am getting an error on the code in Page 76:
if let toShorten = webView.request.URL.absoluteString {
which states 'NSURLRequest?' does not have a member named 'URL'.
I have tried googling an answer, but unfortunately have not come across anything. Any response I can find seems to suggest that my code ought to be working (e.g. How to get url which I hit on UIWebView?). This seems to have the closest answer SWIFT: Why I can't get the current URL loaded in UIWebView? but the solution does not appear to work for me. If I add a ? after the request, it will then at least build it, but I then have a nil variable returned.
I am using Xcode v6.1.1. Here is the piece of code that is coming up with the error in ViewController.swift:
let GoDaddyAccountKey = "0123456789abcdef0123456789abcdef" //this is replaced by my actual account key in my own code
var shortenURLConnection: NSURLConnection?
var shortURLData: NSMutableData?
#IBAction func shortenURL( AnyObject ) {
if let toShorten = webView.request?.URL.absoluteString { // ? now added
let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
shortURLData = NSMutableData()
if let firstrequest = NSURL(string: urlString) //added if here and removed !
let request = NSURLRequest(URL:firstrequest)
shortenURLConnection = NSURLConnection(request:request, delegate:self)
shortenButton.enabled = false
}
}
}
If you have any suggestions on how I can fix this, I would really appreciate it!
Update:
Following suggestions from Ashley below, I have amended my code so that it is no longer bringing up the error (see comments above). However, it is now no longer running. This appears to be because the urlString is being created as http://api.x.co/Squeeze.svc/text/d558979bb9b84eddb76d8c8dd9740ce3?url=Optional("http://www.apple.com/"). The problem is therefore the Optional() that is included and thus makes it an invalid URL. Does anyone have a suggestion on how to remove this please?
request is an optional property on UIWebView:
var request: NSURLRequest? { get }
also stringByAddingPercentEscapesUsingEncoding returns an optional:
func stringByAddingPercentEscapesUsingEncoding(_ encoding: UInt) -> String?
What you need is to make user of optional binding in a few places:
if let toShorten = webView.request?.URL.absoluteString {
if let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
shortURLData = NSMutableData()
if let firstrequest = NSURL(string: urlString) { // If a method can return a nil, don't force unwrap it
let request = NSURLRequest(URL:first request)
shortenURLConnection = NSURLConnection(request:request, delegate:self)
shortenButton.enabled = false
}
}
}
See Apple's docs on optional chaining for details
See Apple's docs for NSURL class