4

I have one array like

var arraybtnState: NSArray! = NSArray()  

For add/Remove object into array, I had cast it in to NSMutableArray...

(self.arraybtnState as! NSMutableArray).addObject(button.tag)  
(self.arraybtnState as! NSMutableArray).removeObject(button.tag)  

But it gives me SIGABRT error:

Could not cast value of type 'Swift._SwiftDeferredNSArray' (0x453e08) to 'NSMutableArray'

I have already tried NSMutableArray but it gives me error in below line

self.arraybtnState = self.strSubscribe?.componentsSeparatedByString(",") 

The question is what is preferred way to add/remove object in array

0

4 Answers 4

3

You should do it in a different way

var arraybtnState = [Int]()
arraybtnState.append(5)
arraybtnState.append(6)
arraybtnState.removeAtIndex(0)

While you declaring it like so

 var arraybtnState:NSArray! = NSArray()  

you are creating pure NSArray type and it indeed cannot be casted to NSMutableArray

UPDATE

You can deal with it as follows

let some = self.strSubscribe?.componentsSeparatedByString(",").map{
    (var char) -> Int in
    return char.toInt() ?? 0
}
arraybtnState += some
Sign up to request clarification or add additional context in comments.

5 Comments

check out the update...if i do this ....the error comes in componentseperatedbycomma line
error on last line Binary operator '+=' cannot be applied to operands of type '[(Int)]' and '[Int]?
Maybe you adding it not to my code snippet variable? It works for me well
is self.strSubscribe a String?
3

An NSArray is immutable. Once created, it can't be modified.

That's the purpose of NSMutableArray, to be modifiable.

var myArr = NSMutableArray()
myArr.addObject("hey")

You also could use a Swift typed array:

var swiftArray = [String]()
swiftArray.append("hey")

EDIT:

There's something screwy going on with the compiler. I think there's a bug in Xcode 6.3/Swift 1.2.

This code should work:

let aString:String? = "One,Two,Three" let array = aString?.componentsSeparatedByString(",") as NSArray

But it complains

[String]? is not convertible to 'NSArray'

Suggesting that in Swift, componentsSeparatedByString returns a string array optional ([String]?]).

If that was the case, you should be able to fix it like this:

let array = aString?.componentsSeparatedByString(",")! as NSArray

But that gives this error:

error: operand of postfix '!' should have optional type; type is '[String]'

Very strange.

If you look in the headers, the Swift definition of componentsSeparatedByString() is

func componentsSeparatedByString(separator: String) -> [String]

It's pretty clear that componentsSeparatedByString() returns a Swift String array, not an optional. The compiler complains with either syntax.

This code does work:

let string1:String? = "One,Two,Three"
let array1 = string1?.componentsSeparatedByString(",")
let aMutableArray = (array1! as NSArray).mutableCopy()
  as! NSMutableArray

That is your solution.

5 Comments

I tried NSMutableArray, but the problem is i also want to add this line....self.arraybtnState = self.strSubscribe?.componentsSeparatedByString(",")....and NSMutableArray gives me error on this line
or even var array:[Int] = []
That is a separate problem. If you make arraybtnState a mutable array (either a Swift var array or an NSMutableArray) then you can't replace it with an immutable array. That's what your code self.arraybtnState = self.strSubscribe?.componentsSeparatedByString(",") does. The method componentsSeparatedByString creates an immutable array. Change that line to self.arraybtnState = self.strSubscribe?.componentsSeparatedByString(",").mutableCopy().
@DuncanC it gives me error that string does not have member mutableCopy
See my edits. It looks like there's a compiler bug. AT the bottom of my edits I provide code that DOES Work.
2

Please write in selected cell function. it is Automatically delete cell. when you select cell

arryName.remove(indexPath.row)

Comments

-1

NSArray is immutable. It can't be modified after declaration.

Create NSMutableArray, to be modifiable.

Remove Value at Index

someMutableArray.removeAtIndex(2)

This will remove the value at index 2.

Remove at End of Array

someMutableArray.removeLast()   

Remove All Values

someMutableArray.removeAll()

Removes all values from array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.