1

I'm trying to create a array of dictionary<String,NSImage>,
so I do this:

class PreferencesController : NSObject{

var generalImage = NSImage(named: "NSAdvanced");
var textEditingImage = NSImage(named: "NSFontPanel");
var GUIImage = NSImage(named: "NSColorPanel");

var preferencesOptionTableList : NSMutableArray = [
    ["name":"General"],
    ["name":"Text Editing"],
    ["name":"GUI"]];

var preferencesOptionTableImageList : [[String:NSImage]] = [["image":textEditingImage]];
}

But I got this error:

PreferencesController.Type does not have a member named 
'textEditingImage'

Please help me I still don't know what to do after a hour of search

1 Answer 1

1

I see two possible problems:

  1. textEditingImage is most likely an optional NSImage?, not NSImage, which will most likely be a problem when you put it into the dictionary. I suspect you will need to unwrap it first.
  2. The var preferencesOptionTableList : NSMutableArray... should be split into declaration and definition and the definition moved to init, possibly like this:

    class PreferencesController: NSObject {
    
    
    var textEditingImage:NSImage = NSImage(named: "NSFontPanel")!
    
    var preferencesOptionTableImageList : [[String:NSImage]]
    
    override init() {
        preferencesOptionTableImageList = [["image":textEditingImage]]
        super.init()
     }
    }
    
4
  • Hi, thanks for reply. I tried this "var preferencesOptionTableImageList = [NSDictionary(object: textEditingImage?, forKey: "image")]; " but still the same error
    – QI HaoYan
    Feb 9, 2015 at 4:22
  • @Ql HaoYan, adding a ? at the end of an optional only unwraps it for chained events. Try var textEditingImage = NSImage(named: "NSFontPanel")! Feb 9, 2015 at 4:25
  • Also, note remove the ; at the end of all your statements and you don't have to specify constant or variable types unless you HAVE to specify them.... Swift is not Obj-C Feb 9, 2015 at 4:27
  • That works, thank you very much. But I'm wondering why when I put "preferencesOptionTableImageList = [["image":textEditingImage]]" out of "init()" I got error "expected declaration"
    – QI HaoYan
    Feb 9, 2015 at 4:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.