xcode 不能在 swift 中不同大小的类型之间进行 unsafeBitCast

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31717580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:35:10  来源:igfitidea点击:

can't unsafeBitCast between types of different sizes in swift

iosarraysxcodeswift

提问by Alexander Khitev

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.

当我尝试在数组中查找重复项时,出现错误“不能在不同大小的类型之间进行 unsafeBitCast”。我发现以下方法重复。

    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

我在此链接上知道的查找重复项的方法在此处输入链接描述。我使用 Xcode 6 和 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(){ 
var array = ["Apple", "Mac", "iPhone", "iPad Air", "Apple", "Air", "Air"]
var filteredArray = Array(Set(array))

println(filteredArray)
.lastPathComponent }.filter(){
var mp3Files: Array<String!>!
.pathExtension == "mp3" } println("mp3 files \(mp3Files)") }

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

当我在操场上写这段代码时,它可以工作。一个例子

var arrayForSearch = mp3Files as! [String]

How can I use it in my project?

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

采纳答案by Rob Napier

import UIKit

class ViewController: UIViewController {

    let strArr = ["a","b","a","d","f","f","b"]

    override func viewDidLoad() {
        super.viewDidLoad()

        filter()
    }

    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 = strArr
        var newArr = uniq(arrayForSearch)
        println("filtered array \(newArr)")
    }
}

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

哇,这么多感叹号....它们不是必需的。

filtered array [a, b, d, f]

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

并且 的类型mp3Files永远不能与 相同[String],所以你不能在它们之间强制转换(如果它允许你会崩溃)。

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

您经常使用隐式解包的选项。只有在某些特殊情况下才需要它们。只需更改mp3Files[String](在这种情况下,您根本不需要as!;您也不应该as!经常需要)。

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

同样,arrayFiles(您从未使用过),应该只是[NSURL],而不是Array<NSURL!>!

回答by Dharmesh Kheni

Here I try same code as you given:

在这里,我尝试与您给出的代码相同的代码:

##代码##

And OutPutin console:

OutPut在控制台:

##代码##

My be there is a problem when you are casting your array to [String]so check it once again.

当您将数组转换为时,我会遇到问题,[String]因此请再次检查它。