我是从iOS教程中学到的,但它不起作用
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {
var mediaType = info.objectForKey(UIImagePickerControllerMediaType) as String
var originalImage, editedImage, imageToUse: UIImage
// Handle a still image picked from a photo album
if (CFStringCompare(CFStringRef(mediaType), kUTTypeImage, 0) == CFComparisonResult.CompareEqualTo) {
editedImage = info.objectForKey(UIImagePickerControllerEditedImage) as UIImage
originalImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
if (editedImage) {
imageToUse = editedImage
} else {
imageToUse = originalImage
}
// Do something with imageToUse
}
它经常提醒我
CFStringRef不能与' @lvalue String'
构建
所以我尝试了这个:
// Handle a still image picked from a photo album
var temp = mediaType as CFString
if (CFStringCompare(temp, kUTTypeImage, 0) == CFComparisonResult.CompareEqualTo) {
editedImage = info.objectForKey(UIImagePickerControllerEditedImage) as UIImage
originalImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
它提醒我
无法转换表达式' CFString'输入' $ T1'
答案 0 :(得分:7)
Swift.String
和NSString
自动桥接,NSString
和CFString
可以相互投射,但你不能(现在?)过渡到所有的从Swift String
到CFString
的方式,无需额外演员或两次。
这是一个Swift字符串,一个带CFString
的函数,以及如何通过强制转换为NSString
来调用它:
var str = "Hello, playground"
func takesCFString(s: CFString) {}
takesCFString(str as NSString)
请注意,您不需要在Swift中使用CFStringRef
(并且CFString
API的大多数Swift声明都没有)。编译器利用Xcode 5引入的CF类型的自动ARC桥接,让您可以像Swift(或ObjC)对象一样处理这些类型。
答案 1 :(得分:2)
var str = "string"
var cfstr:CFString = str as NSString // OR str.__conversion()