ios – 如何在swift中的NSMutableArray中将object添加为NSMutableArray

前端之家收集整理的这篇文章主要介绍了ios – 如何在swift中的NSMutableArray中将object添加为NSMutableArray前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将NSMutableArray的addObject添加到NSMutableArray中,所以我该如何在 swift中实现这一点.
并检索它所以请帮助我,因为现在我正在学习迅速.

间接地说我想要2D数组意味着NSMutableArray的NSMutableArray.

//The below 2 lines iterative!
var dict:NSMutableDictionary? = NSMutableDictionary();
 linePointsArray!.addObject(dict!);

// and below 2 line is execute after complete the above iteration
 arrLine!.addObject(linePointsArray!);
 linePointArray!.removeAllObject();

//after that the above whole process is done iterative.

解决方法

如果我没有错,你想在另一个NSMutableArray中添加一个NSMutableArray的对象,你可以通过以下代码来实现
var myArray : NSMutableArray = ["Hello"]
var arrayOfArray : NSMutableArray = [myArray]

// Insert more arrays with insertObject or addObject

arrayOfArray.insertObject(myArray,atIndex: 0)
arrayOfArray.addObject(myArray)

//Retrive
var newArray:NSMutableArray = arrayOfArray[0] as NSMutableArray
println("\(newArray)")

对于Swift 3:

var myArray : NSMutableArray = ["Hello"]
var arrayOfArray : NSMutableArray = [myArray]

// Insert more arrays with insertObject or addObject

arrayOfArray.insert(myArray,at: 0)
arrayOfArray.add(myArray)

//Retrive
var newArray:NSMutableArray = arrayOfArray[0] as! NSMutableArray
print("\(newArray)")
原文链接:https://www.f2er.com/iOS/336566.html

猜你在找的iOS相关文章