本文介绍了在使用通用迅速阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是pretty简单。我有一个总是给我UInt16s的数据源。我得到这个原始数据不同的数据集得出结果。一些派生的数据集是浮动的,有些是UInt8s,有些是UInt16s。

我排队在那里后来我的绘图类检索的数据导出。

队列是数组的数组,看起来像这样:[[UINT16],[浮动]或[UINT8]

我想利用仿制药,但我得到一个编译错误,当我尝试添加一个通用类型的数组的声明数组为[AnyObject]

由于我学斯威夫特,我不断碰到这种AnyObject /通用的问题还真不少。任何帮助/洞察力是AP preciated。

  Base类:NSObject的{   VAR队列:[AnyObject] =阵列()   FUNC addtoQueue< T>(数据集:[T]){
      queue.append(数据集)
   }   FUNC removeFromQueue() - > [AnyObject] {
      返回queue.removeAtIndex(0)
   }
}类数据集1:基{   FUNC getSomeData(RAWDATA:[UINT16]){
      VAR结果:[浮点] = processRawData(RAWDATA)
      addToQueue(结果)
   }
}


解决方案

这可能是因为你不明白AnyObject是什么。这是所有的的自动采用的协议类型。但浮动,UINT16和UINT8是的的班;他们的结构

这可能是因为你的意思是 [任何] 为您的阵列的类型。在这种情况下,你并不需要一个通用的。这工作:

  VAR队列:[任何] =阵列()
FUNC addToQueue(数据集:[任意]){
    queue.append(数据集)
}
令f =浮动(1)
让I1 = UINT8(2)
让I2 = UINT16(3)
addToQueue(苯并[f])
addToQueue([I1])
addToQueue([12])

如果你坚持 [AnyObject] ,就有两个问题:

您一般是太普通。在宇宙中并非一切是一个AnyObject,所以编译器怎么知道这件事会的的一个AnyObject?写下您的一般是这样的:

  FUNC addToQueue< T:AnyObject>(数据集:[T]){
    queue.append(数据集)
}

现在的编译器知道只有符合AnyObject东西会调用这个方法时使用。在这一点上,但是,这是一个有点难以看到您的通用是什么;你是不是对采用T别处,所以只写一个正常功能:

  FUNC addToQueue(数据集:[AnyObject]){
    queue.append(数据集)
}

第二个问题是,你仍然将不得不转换(如德鲁的答案告诉你),因为没有AUTOMAGIC之间,也就是说,一个UIInt16和AnyObject桥接。浮子 _ObjectiveCBridgeable ,但UINT16 UINT8并没有。

  VAR队列:[AnyObject] =阵列()
FUNC addToQueue(数据集:[AnyObject]){
    queue.append(数据集)
}
令f =浮动(1)
让I1 = UINT8(2)
让I2 = UINT16(3)
addToQueue(苯并[f])
addToQueue([NSNumber的(unsignedChar:I1)])
addToQueue([NSNumber的(unsignedShort的:I2)])

This should be pretty simple. I have a data source that always gives me UInt16s. I derive different data sets from this raw data and plot the results. Some of the derived data sets are Floats, some are UInt8s, and some are UInt16s.

I queue the derived data where it is later retrieved by my graphing classes.

Queues are arrays of arrays and look like this: [[UInt16]], [[Float]], or [[UInt8]].

I'm trying to make use of generics, but I get a compiler error when I try to append a generic-typed array to an array that is declared to be [[AnyObject]].

As I'm learning Swift, I keep bumping into this AnyObject / generic problem quite a bit. Any help/insight is appreciated.

class Base: NSObject {

   var queue : [[AnyObject]] = Array()

   func addtoQueue<T>(dataSet: [T]) {
      queue.append(dataSet)
   }

   func removeFromQueue() -> [AnyObject]? {
      return queue.removeAtIndex(0)
   }
}

class DataSet1 : Base {

   func getSomeData(rawData: [UInt16]) {
      var result : [Float] = processRawData(rawData)
      addToQueue(result)
   }
}
解决方案

It may be that you don't understand what AnyObject is. It is the protocol type automatically adopted by all classes. But Float, UInt16, and UInt8 are not classes; they are structs.

It may be that you meant [[Any]] as the type of your array. In that case, you don't need a generic. This works:

var queue : [[Any]] = Array()
func addToQueue(dataSet:[Any]) {
    queue.append(dataSet)
}
let f = Float(1)
let i1 = UInt8(2)
let i2 = UInt16(3)
addToQueue([f])
addToQueue([i1])
addToQueue([i2])

If you insist on [[AnyObject]], you have two problems:

Your generic is too generic. Not everything in the universe is an AnyObject, so how can the compiler know that this thing will be an AnyObject? Write your generic like this:

func addToQueue<T:AnyObject>(dataSet:[T]) {
    queue.append(dataSet)
}

Now the compiler knows that only something conforming to AnyObject will be used when calling this method. At that point, however, it is a little hard to see what your generic is for; you are not using T elsewhere, so just write a normal function:

func addToQueue(dataSet:[AnyObject]) {
    queue.append(dataSet)
}

The second problem is that you will still have to convert (as Drew's answer tells you), because there is no automagic bridging between, say, a UIInt16 and an AnyObject. Float is _ObjectiveCBridgeable, but UInt16 and UInt8 are not.

var queue : [[AnyObject]] = Array()
func addToQueue(dataSet:[AnyObject]) {
    queue.append(dataSet)
}
let f = Float(1)
let i1 = UInt8(2)
let i2 = UInt16(3)
addToQueue([f])
addToQueue([NSNumber(unsignedChar: i1)])
addToQueue([NSNumber(unsignedShort: i2)])

这篇关于在使用通用迅速阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:27