Swift - Insert [AnyObject] into [NSMutableDictionnary]

Hello,


I am trying to build an app using tableView with two columns (one displaying strings, the other one a CheckBox for each string).

The strings are coming from a [AnyObject] variable.

I have try so many things to insert the result of [AnyObject] into the NsMutableDictionnary (which is used by NsTableView) but none of them works.

I have to build the dic manually with the right number of object (related to the result of [AnyObject]) to make it work but I would like the dic to build itself automatically.

Here is an exemple of the code, working if [AnyObject] is made with 4 elements. The TableView column names are "jobs" and "unload" :


import Cocoa

@NSApplicationMain

class AppDelegate: NSObject, NSApplicationDelegate,NSTableViewDataSource,NSTableViewDelegate {


@IBOutlet weak var window: NSWindow!

@IBOutlet weak var myTableView: NSTableView!

var dataArray:[NSMutableDictionary] = [["jobs": "", "unload": 0],["jobs": "", "unload": 0],["jobs": "", "unload": 0],["jobs": "", "unload": 0]];


func applicationDidFinishLaunching(aNotification: NSNotification) {

...

...

...

var ctlelems:[AnyObject] = grepOut!.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())

dataArray = [["jobs": "ctlelems[0]", "unload": 0],["jobs": "ctlelems[1]", "unload": 0],["jobs": "ctlelems[2]", "unload": 0],["jobs": "ctlelems[3]", "unload": 0]];

}

func applicationWillTerminate(aNotification: NSNotification) {

}

func numberOfRowsInTableView(aTableView: NSTableView) -> Int

{

return dataArray.count

}

func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject?

{

let object = dataArray[row] as NSMutableDictionary

if ((tableColumn!.identifier) == "unload")

{

println(object)

return object[tableColumn!.identifier] as? Int!

}

else

{

return object[tableColumn!.identifier] as? String!

}

}

func tableView(tableView: NSTableView, setObjectValue object: AnyObject?, forTableColumn tableColumn: NSTableColumn?, row: Int)

{

dataArray[row].setObject(object!, forKey: (tableColumn?.identifier)!)

}

}



I would like to do the following,


var x: Int = 0

for lines in ctlelems {

dataArray = [["jobs": ctlelems[x], "unload": 0]];

x=x+1

}


but it is not working.

Most of the time I get "Found nill" error, or only the last element of [AnyObject] is displayed.

Could someone help me please?

This code:

            dataArray = [["jobs": ctlelems[x], "unload": 0]];

is replacing the whole content of dataArray with the single element array. It does not work as "Insert".

To append an element to Array:

            dataArray += [["jobs": ctlelems[x], "unload": 0]]

or

            dataArray.append(["jobs": ctlelems[x], "unload": 0])


"Found nil" is another problem. You use too much unwrapping operators ("!"), without checking the optional value is nil or not.

Put nil checking code before using "!".

Thank you for this answer, it is working better now (but not yet perfectly).

If I try the first exemple, I get the following error :

Binary operator '+=' cannot be applied to operands of type '[NSMutableDictionary]' and '[Dictionary<String, AnyObject>]'


If I use .append, the elements are properlly added to the dictionnary :

var x: Int = 0
        for lines in ctlelems
        {
            dataArray.append (["jobs": ctlelems[x], "unload": 0]);
            x=x+1
            }


Gives :

[{

jobs = "element1";

unload = 0;

}, {

jobs = "element2";

unload = 0;

}]


But, println(dataArray.count) Is reporting only one element (1)

func numberOfRowsInTableView(aTableView: NSTableView) -> Int
    {
        println(dataArray.count)
        return dataArray.count
    }


And

println(object)

which is inside {... objectValueForTableColumn...} is reporting the following :

{

jobs = "";

unload = 0;

}, {

jobs = "";

unload = 0;

}

I have initialize dataArray using the following code, is it correct?:

var dataArray:[NSMutableDictionary] = [["jobs": "", "unload": 0]];


Regards.

Binary operator '+=' cannot be applied to operands of type '[NSMutableDictionary]' and '[Dictionary<String, AnyObject>]'

Sorry, I have missed checking, it seems you need explicit casting in this case.

            dataArray += [["jobs": ctlelems[x], "unload": 0] as NSMutableDictionary]


But, println(dataArray.count) Is reporting only one element (1)

Well, the result depends on where the code conaining `dataArray.append` is put, and when the output is generated.


I have initialize dataArray using the following code, is it correct?:

Syntactically, it's correct. But I cannot say anything about why you cannot get expected results, with that single line of code.

Swift - Insert [AnyObject] into [NSMutableDictionnary]
 
 
Q