如何在swift中的NSMutableArray中将对象添加为NSMutableArray

时间:2014-10-09 10:14:26

标签: ios swift nsmutablearray

我想addObject NSMutableArray NSMutableArray进入NSMutableArray,所以我怎样才能迅速实现这一目标。 并检索它,所以请帮助我,因为我现在正在快速学习。

间接地说我希望2D数组意味着NSMutableArray的{​​{1}}。

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

3 个答案:

答案 0 :(得分:36)

如果我没有错,你想在另一个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)")

答案 1 :(得分:8)

以下是一个例子:

var mutableArray: NSMutableArray = []
var nestedMutableArray: NSMutableArray = []
mutableArray.addObject(nestedMutableArray)
var retrievedNestedMutableArray:NSMutableArray = mutableArray[0] as NSMutableArray

答案 2 :(得分:0)

var nameArray: NSMutableArray = ["Hello", "I", "am", "Smith"] var arrayOfnameArray: NSMutableArray = [nameArray]

arrayOfArray.add(nameArray)

//检索 var newArray:NSMutableArray = arrayOfnameArray[0] as! NSMutableArray print("\(newArray)")