简体   繁体   English

swift函数返回一个数组

[英]swift function returning an Array

Im learning Swift and I can understand how to create a simple function that takes in an Array and returns an Array. 我正在学习Swift,我可以理解如何创建一个接收数组并返回数组的简单函数。 Heres my code: 继承我的代码:

 func myArrayFunc(inputArray:Array) -> Array{

 var newArray = inputArray

// do stuff with newArray

 return newArray
 }

The red error I get is: Reference to generic type 'Array" requires arguments in <> 我得到的红色错误是:对泛型类型'Array'的引用需要<>中的参数

In Swift Array is generic type, so you have to specify what type array contains. 在Swift Array是泛型类型,因此您必须指定数组包含的类型。 For example: 例如:

func myArrayFunc(inputArray:Array<Int>) -> Array<Int> {}

If you want your function to be generic then use: 如果您希望您的函数是通用的,那么使用:

func myArrayFunc<T>(inputArray:Array<T>) -> Array<T> {}

If you don't want to specify type or have generic function use Any type: 如果您不想指定类型或具有泛型函数,请使用Any类型:

func myArrayFunc(inputArray:Array<Any>) -> Array<Any> {}

Depends on what is it exactly you want to do. 取决于你想要做什么。 If you want a specialized function that takes an array of a specific type MyType, then you could write something like: 如果你想要一个带有特定类型MyType数组的专用函数,那么你可以编写如下内容:

func myArrayFunc(inputArray: [MyType]) -> [MyType] {
    // do something to inputArray, perhaps copy it?
}

If you want a generic array function, then you have to use generics. 如果你想要一个通用数组函数,那么你必须使用泛型。 This would take an array of generic type T and return an array of generic type U: 这将采用泛型类型T的数组并返回泛型类型U的数组:

func myGenericArrayFunc<T, U>(inputArray: [T]) -> [U] {

}

There is no such thing as an Array in Swift, but there are arrays of a certain type, so you should give the function a generic type, like in: Swift中没有Array这样的东西,但是有某种类型的数组,所以你应该给函数一个泛型类型,比如:

func myArrayFunc<T>(inputArray:Array<T>) -> Array<T>{  
   // do what you want with the array
}

and then call it by instantiating T to a specific type, and passing an array of such type. 然后通过将T实例化为特定类型并传递此类型的数组来调用它。

thanks all (especially Kirsteins). 谢谢所有人(特别是Kirsteins)。 So I've come up with this example that works well and looks logical: 所以我想出了这个效果很好并且看起来合乎逻辑的例子:

func myArrayFunc(inputArray:Array<String>) -> Array<String>{

var newArray = inputArray

// do stuff with newArray

return newArray
}

This should do it: 这应该这样做:

func myArrayFunc<T>(inputArray:Array<T>) -> Array<T> {

  var newArray = inputArray

  // do stuff with newArray

  return newArray
}

You declare the generic type T , which is just a placeholder. 您声明泛型类型T ,它只是一个占位符。 Because it has no requirements T can be replaced by any type (when the function is called). 因为它没有要求T可以被任何类型替换(当调用函数时)。 So your function could be called like this: 所以你的函数可以这样调用:

myArrayFunc([1, 2, 3])

or this: 或这个:

myArrayFunc(["a", "b", "c"])

The preferred syntax is generally [T] rather than Array<T> , though. 但是,首选语法通常是[T]而不是Array<T> (although both are correct) (虽然两者都是正确的)

func myArrayFunc<T>(inputArray: [T]) -> [T] {

  var newArray = inputArray

  // do stuff with newArray

  return newArray
}

Try this 试试这个

var test = doArray([true,true,true])

test.count
func doArray(arr : [AnyObject]) -> [AnyObject] {

   var _arr = arr

return _arr
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM