ios 返回数组的 swift 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31268209/
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-08-31 06:46:23  来源:igfitidea点击:

swift function returning an Array

iosarraysswift

提问by pete

Im learning Swift and I can understand how to create a simple function that takes in an Array and returns an Array. Heres my code:

我正在学习 Swift,我可以理解如何创建一个简单的函数,它接受一个数组并返回一个数组。这是我的代码:

 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 <>

我得到的红色错误是:对泛型类型“数组”的引用需要 <> 中的参数

回答by Kirsteins

In Swift Arrayis generic type, so you have to specify what type array contains. For example:

在 Swift 中Array是泛型类型,所以你必须指定数组包含什么类型。例如:

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 Anytype:

如果您不想指定类型或使用泛型函数,请使用Any类型:

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

回答by Valentin

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] {

}

回答by pete

thanks all (especially 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
}

回答by Renzo

There is no such thing as an Arrayin Swift, but there are arrays of a certain type, so you should give the function a generic type, like in:

Array在 Swift 中没有像 an 这样的东西,但是有某种类型的数组,所以你应该给函数一个泛型类型,比如:

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 实例化为特定类型并传递此类类型的数组来调用它。

回答by oisdk

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. Because it has no requirements Tcan be replaced by any type (when the function is called). So your function could be called like this:

您声明了泛型类型T,它只是一个占位符。因为它没有要求T可以替换为任何类型(当函数被调用时)。所以你的函数可以这样调用:

myArrayFunc([1, 2, 3])

or this:

或这个:

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

The preferred syntax is generally [T]rather than Array<T>, though. (although both are correct)

不过,首选的语法通常是[T]而不是Array<T>。(虽然两者都是正确的)

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

  var newArray = inputArray

  // do stuff with newArray

  return newArray
}

回答by halfred

Try this

尝试这个

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

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

   var _arr = arr

return _arr
}