Dictionary now gives error 'not convertible to BooleanLiteralConvertible' since updating to Swift 1.2
From the Xcode 6.3 release notes:
The implicit conversions from bridged Objective-C classes
(NSString/NSArray/NSDictionary) to their corresponding Swift value
types (String/Array/Dictionary) have been removed, making the Swift
type system simpler and more predictable.
The problem in your case are the CFString
s like kCGImageSourceThumbnailMaxPixelSize
. These are not automatically
converted to String
anymore. Two possible solutions:
let options = [
kCGImageSourceThumbnailMaxPixelSize as String : maxSize,
kCGImageSourceCreateThumbnailFromImageIfAbsent as String : true
]
or
let options : [NSString : AnyObject ] = [
kCGImageSourceThumbnailMaxPixelSize: maxSize,
kCGImageSourceCreateThumbnailFromImageIfAbsent: true
]
Merging Dictionaries with multiple Types, or Any / AnyObject Type
You don't actually need to (and you can't) specify the Any
type in the function declaration - you need to have the subtypes of the newValues
dictionary match the dictionary it's extending:
extension Dictionary {
mutating func extend(newVals: Dictionary) {
for (key,value) in newVals {
self.updateValue(value, forKey:key)
}
}
}
var dict = ["one": 1, "two": 2, "hello": "goodbye"]
dict.extend(["three": 3])
dict.extend(["foo": "bar"])
Key
and Value
are type aliases within the Dictionary
type that map to the specific key- and value-types of a particular instance.
Creating a CFDictionary
Here is your working code:
func processImage(jpgImagePath: String, thumbSize: CGSize) {
if let path = NSBundle.mainBundle().pathForResource(jpgImagePath, ofType: "") {
if let imageURL = NSURL(fileURLWithPath: path) {
if let imageSource = CGImageSourceCreateWithURL(imageURL, nil) {
let maxSize = max(thumbSize.width, thumbSize.height) / 2.0
let options : [NSString : AnyObject] = [
kCGImageSourceThumbnailMaxPixelSize: maxSize,
kCGImageSourceCreateThumbnailFromImageIfAbsent: true
]
let scaledImage = UIImage(CGImage: CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options))
// do other stuff
}
}
}
}
From Docs:
The implicit conversions from bridged Objective-C classes
(NSString/NSArray/NSDictionary) to their corresponding Swift value
types (String/Array/Dictionary) have been removed, making the Swift
type system simpler and more predictable.
The problem in your case are the CFStrings
like kCGImageSourceThumbnailMaxPixelSize
. These are not automatically converted to String anymore.
reference from HERE.
Can Swift return value from an async Void-returning block?
You should employ asynchronous (ie, escaping) completion handler yourself:
class func checkIfUserExists(uid: String, completion: @escaping (Bool) -> Void) {
userRef.childByAppendingPath(uid).observeSingleEventOfType(.Value) { snapShot in
if snapShot.value is NSNull {
completion(false)
} else {
completion(true)
}
}
}
You can then call this like so:
MyClass.checkIfUserExists(uid) { success in
// use success here
}
// but not here
In your revised question, you demonstrate the use of dispatch groups to make this asynchronous method behave synchronously. (Semaphores are also often used to the same ends.)
Two issues:
This will deadlock if they dispatch their completion handler back to the main queue (and in many cases, libraries will do this to simplify life for us), because you're coincidentally blocking the very same thread they're trying to use. I don't know if that's what they've done here, but is likely.
If you want to confirm this, temporarily remove dispatch group and then examine
NSThread.isMainThread
and see if it's running in main thread or not.You never should block the main thread, anyway. They provided an asynchronous interface for good reason, so you should use asynchronous patterns when calling it. Don't fight the asynchronous patterns, but rather embrace them.
Related Topics
Swiftui Hierarchical Picker with Dynamic Data
How to Sort JSON Coming from Alamofire and Return Final JSON Object (Swiftyjson)
Swift Notification Fire from Datepicker
Create Generic Delegate for Class
Reflection with Swift - Get Functions Name of a Class
Swift Threading with Dispatchgroup
Synchronized Realm - Airplane Mode
How to Play Avplayeritems Immediately
Consuming a Soap Web Service with Swift
Self Refrence Inside Swift Closure Return Nil Some Time
How to Prevent Eventstore Access Error on First Run
Swift: Visual Glitches When Presenting a Main and Alternative (Login/Onboarding) Flow
Struggling with Notificationcenter/Combine in Swiftui/Avplayer
Com.Apple.Itunes Aedeterminepermissiontoautomatetarget Is Always Return -600
Swift Safely Unwrapping Optinal Strings and Ints
Swift 3:Appdelegate Does Not Conform to Protocol Gidsignindelegate