createFileAtPath not accepting NSURL anymore - swift2

let singleImage = "current.jpg"
let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(singleImage)
let fileManager = NSFileManager.defaultManager()
fileManager.createFileAtPath(path, contents: imageDataFromURL, attributes: [:])
createFileAtPath stopped working after upgrade to Swift 2 and now it gives the following error:
Cannot convert value of type 'NSURL' to expected argument type 'String'

Unfortunately NSFileManager has no creation method that operates on a NSURL. In my code I try to avoid String for path usage and this is one of the rare places I still fall back to a path using the NSURL.path property
fileManager.createFileAtPath(pathURL.path!, contents: imageDataFromURL, attributes: [:])

Related

Ambiguous use of ‘subscript’ error when importing AVFoundation

Using Xcode 7.2 and Swift 2.1.1
I am retrieving data from a .plist file
The file contains data for a series of quizzes. The data for the quiz to be retrieved consists of an array of 12 questions and a corresponding array of 12 multiple choice options (4 members each).
var quizId = “”
var questions:[String] = []
var answers:[[String]] = []
The quiz id is passed in a segue from the previous view controller. Then the data is retrieved in ViewDidLoad.
let path = NSBundle.mainBundle().pathForResource(“quiz id”, ofType: "plist")
let dict = NSDictionary(contentsOfFile: path!)
questions = dict!.objectForKey(“Questions”)![0] as! [String]
answers = dict!.objectForKey(“Answers”)![1] as! [[String]]
The code works perfectly well until I try to import AVFoundation, when the last two lines throw the ambiguous use of ‘subscript’ error.
This is because importing AVFoundation brings up new subscript definitions (namely AUAudioUnitBusArray, thank you Martin R.) and it confuses the compiler which doesn't know what type is dict!.objectForKey(“Questions”) anymore (it is indeed inferred as AnyObject after the import, instead of NSArray).
The cure is to safely help the compiler to know the type, for example by doing a downcast with optional binding:
if let questions = dict?.objectForKey("Questions") as? NSArray {
print(questions[0])
}
or even better:
if let questions = dict?.objectForKey("Questions") as? [String] {
print(questions[0])
}

Cannot invoke initializer for type 'NSSet' with an argument list of type '(array: [NSData?])'

Getting this error when updating to swift 2.0:
Cannot invoke initializer for type 'NSSet' with an argument list of type '(array: [NSData?])'
I already do the searching but still can't fix it.
var object:UIImageView = UIImageView(frame: CGRectMake(0, 0, result.size.width, result.size.height))
object.image = UIImage(named: "image_default")
let element = NSEntityDescription.insertNewObjectForEntityForName("XKIMAGEVIEW", inManagedObjectContext: CoreDataUtil.sharedInstance.managedObjectContext!) as! XKIMAGEVIEW
element.image = NSSet(array:[UIImageJPEGRepresentation(object.image!, 1)])
the last line just have the error
seems like NSData couldn't be included in the array
Any idea of how to fix this issue?
UIImageJPEGRepresentation returns an optional, NSSet doesn't accept optionals.
Just unwrap the optional:
NSSet(array:[UIImageJPEGRepresentation(object.image!, 1)!])
Or, if the image representation could be nil use optional bindings
if let imageData = UIImageJPEGRepresentation(object.image!, 1) {
element.image = NSSet(array:[imageData])
}

Cannot subscript a value of type nsobject anyobject with an index of type String

I want to get name from google places using address dictionary in my xcode 7 project
Here is my code :
let name = placemark.addressDictionary["Name"] as? String
let city = placemark.addressDictionary["City"] as? String
let state = placemark.addressDictionary["State"] as? String
In otherwords NSObject and AnyObject are not subscript able with a string.
It's not the clearest message.
You might try NSString subscripts or you might not be able to use subscript syntax without more type info to tell Mr. compiler that your objects can respond to subscript syntax

What's the correct syntax of copying a UIViewController?

I would like to copy an instance of a UIViewController.
Something like this:
gOriginViewController = self.copy() as! DiscoveryViewController
Which gives me the runtime error:
Bliss2.DiscoveryViewController copyWithZone:]: unrecognized selector sent to instance 0x7f8b4440cd70'
What's the correct syntax of doing a copy?
UIViewController does not implement NSCopying, and thus cannot be copied using copy(). You can use NSKeyedArchiver to do this:
let data = NSKeyedArchiver.archivedDataWithRootObject(self)
let copy = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! DiscoveryViewController

Writing a String to an NSOutputStream in Swift

I'm trying to write a String to an NSOutputStream in Swift. Writing Strings that way with Objective C usually works by passing it as NSData
NSData *data = [[NSData alloc] initWithData:[mystring dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
This does not work with swift
var data: NSData = mystring.dataUsingEncoding(NSUTF8StringEncoding)!
outputStream.write(data, maxLength: data.length)
this yields the error
'NSData' is not convertible to 'UnsafePointer'
for the line that writes the data to the stream.
How would you write a String to an NSOutputStream in Swift?
In recent Swift it should be even easier and NSData is no longer needed.
let s = "String to encode"
let encodedDataArray = [UInt8](s.utf8)
outputstream.write(encodedDataArray, maxLength: encodedDataArray.count)
Arrays can be accessed as buffers of the correct their type (see withUnsafeBufferPointer). I think the array is necessary because the utf8 view is not actually instantiated as a full array but just a view into the original string.
In production code you should check the return value of the write to the output stream and depending on your scenario check there is space before the write but the focus of this answer is the encoding of the Swift String so that it can be written.
There are two issues here. The first is that you're passing data to outputStream.write() and not data.bytes (like you passed [data bytes] in your Objective-C code). The second issue is that data.bytes returns an UnsafePointer<Void>, but NSOutputStream.write() takes an UnsafePointer<UInt8>. Luckily, UnsafePointer has a way to convert between types:
/// Convert from a UnsafePointer of a different type.
///
/// This is a fundamentally unsafe conversion.
init<U>(_ from: UnsafePointer<U>)
Putting those things together makes your code look something like this:
let data: NSData = mystring.dataUsingEncoding(NSUTF8StringEncoding)!
outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
Hope this example helps out. It writes a string to document directory. Note that outputStream is tested using Swift 1.1 with failable initializer.
var myString = "Hello world!"
var docPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var path = docPath.stringByAppendingPathComponent("doc")
var outputStream = NSOutputStream(toFileAtPath: path, append: false)
var data: NSData = myString.dataUsingEncoding(NSUTF8StringEncoding)!
var buffer = [UInt8](count:data.length, repeatedValue:0)
data.getBytes(&buffer)
outputStream?.open()
outputStream?.write(&buffer, maxLength: data.length)
outputStream?.close()
Regards

Resources