类型SWIFT不同尺寸之间不能unsafeBitCast [英] can't unsafeBitCast between types of different sizes in swift

查看:904
本文介绍了类型SWIFT不同尺寸之间不能unsafeBitCast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图找到阵副本,我得到的错误类型的不同尺寸之间不能unsafeBitCast。我发现下面的方法重复的。

  FUNC uniq的< S:序列类型,T:哈希哪里S.Generator.Element == T>(来源:S) -  GT; [T] {
    VAR缓冲= [T]()
    VAR添加=设置< T>()
    在源ELEM {
        如果!added.contains(ELEM){
            buffer.append(ELEM)
            added.insert(ELEM)
        }
    }
    返回缓冲区
}FUNC过滤器(){
    VAR arrayForSearch = mp3Files的! [串]
    VAR filteredArray =的uniq(arrayForSearch)
    的println(过滤阵列\\(filteredArray))
}

查找重复的方法,我知道这个链接<一个href=\"http://stackoverflow.com/questions/25738817/does-there-exist-within-swifts-api-an-easy-way-to-remove-duplicate-elements-fro\">enter这里的链接描述的。我使用的是X code 6和雨燕1.2

有这个code数组。

  VAR mp3Files:阵列&LT;串GT ;!
FUNC exportData(){
    VAR generalURL:[AnyObject]
    VAR arrayFiles:阵列&LT; NSURL&GT;!
    var目录= fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory,inDomains:NSSearchPathDomainMask.UserDomainMask)
    VAR urlFromDirectory = directory.first的! NSURL    var文件= fileManager.contentsOfDirectoryAtURL(urlFromDirectory,includingPropertiesForKeys:无,选择:NSDirectoryEnumerationOptions.SkipsHiddenFiles,错误:无)!
    的println(文件\\(文件))    mp3Files = file.map(){$ 0.lastPathComponent} .filter(){$ 0.pathExtension ==MP3}  的println(MP3文件\\(mp3Files))
}

当我在操场上写了这个code它的作品。
一个例子

  VAR数组= [苹果,陆委会,iPhone,iPad的空气,苹果,空气,空]
VAR filteredArray =阵列(集(阵列))的println(filteredArray)

我如何使用它在我的项目呢?


解决方案

  VAR mp3Files:阵列&LT;串GT ;!

哇,这是一个很大的感叹号....他们没有必要的。

  VAR arrayForSearch = mp3Files的! [串]

和类型 mp3Files 永远是相同的 [字符串] ,所以你不能强迫它们之间-cast(如果它让你就会崩溃)。

您是使用隐式展开自选方式过于频繁。它们只需要在某些特殊情况下。只要改变 mp3Files [字符串] (在这种情况下你不需要的作为!可言;!你不应该需要经常其一)

同样, arrayFiles (你永远不会使用),应该只是 [NSURL] ,而不是阵列&LT;!NSURL&GT; <!/ code>

when I try to find duplicates in array, I get the error "can't unsafeBitCast between types of different sizes". I find the duplicates following method.

    func uniq<S : SequenceType, T : Hashable where S.Generator.Element == T>(source: S) -> [T] {
    var buffer = [T]()
    var added = Set<T>()
    for elem in source {
        if !added.contains(elem) {
            buffer.append(elem)
            added.insert(elem)
        }
    }
    return buffer
}

func filter() {
    var arrayForSearch = mp3Files as! [String]
    var filteredArray = uniq(arrayForSearch)
    println("filtered array \(filteredArray)")
}

The method of find duplicates I knew on this link enter link description here. I use Xcode 6 and Swift 1.2

There is the array in this code.

var mp3Files: Array<String!>!
func exportData() {
    var generalURL: [AnyObject]?
    var arrayFiles: Array<NSURL!>!
    var directory = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    var urlFromDirectory = directory.first as! NSURL

    var file = fileManager.contentsOfDirectoryAtURL(urlFromDirectory, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil)!
    println("file \(file)")

    mp3Files = file.map(){ $0.lastPathComponent }.filter(){ $0.pathExtension == "mp3" }

  println("mp3 files  \(mp3Files)")
}

When I wrote this code in playground it works. An example

var array = ["Apple", "Mac", "iPhone", "iPad Air", "Apple", "Air", "Air"]
var filteredArray = Array(Set(array))

println(filteredArray)

How can I use it in my project?

解决方案

var mp3Files: Array<String!>!

Wow, that's a lot of exclamation points.... They're not needed.

var arrayForSearch = mp3Files as! [String]

And the type of mp3Files can never be the same as [String], so you can't force-cast between them (and would crash if it let you).

You're using implicitly unwrapped optionals way too often. They are only needed in some special cases. Just change mp3Files to [String] (in which case you won't need the as! at all; you shouldn't need as! very often either).

Similarly, arrayFiles (which you never use), should just be [NSURL], not Array<NSURL!>!.

这篇关于类型SWIFT不同尺寸之间不能unsafeBitCast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆