CFStringCompare重载

时间:2014-06-30 11:22:07

标签: swift cfstring

我正在尝试使用CFStringCompare来比较字符串,但我不断得到Could not find an overload for == that accepts the supplied arguments.更具体地说,代码的位数如下:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!)
{
let mediaType = info.objectForKey(UIImagePickerControllerMediaType) as String


if CFStringCompare(mediaType as NSString!,  kUTTypeMovie, compareOptions: 0) == CFComparisonResult.CompareEqualTo

{

   var moviePath : NSString = info.objectForKey(UIImagePickerControllerMediaURL).path

   if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)
   {

      UISaveVideoAtPathToSavedPhotosAlbum(moviePath, nil, nil, nil)

   }

}
else ... 

我确定我只是搞砸了语法,但我尝试将mediaType设为可选字符串并将其解包:

let mediaType = info.objectForKey(UIImagePickerControllerMediaType) as String?

仍然是could not find overload for ==

有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

调用CFStringCompare()方法不正确(注意:compareOptions是参数而不是名称),请尝试这样:

   let compareResult = CFStringCompare(mediaType as NSString!, kUTTypeMovie, CFStringCompareFlags.CompareCaseInsensitive)

   if compareResult == CFComparisonResult.CompareEqualTo {

              println("Equal")

          }
    else {
              println("Not Equal")

         }

您可以根据需要CFStringCompareFlags(0)提供不同的标记。

如果您没有使用比较标志,可以通过以下方式轻松完成:

if mediaType == kUTTypeMovie {
//Equal
}