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()
     }
    }
    
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for reply. I tried this "var preferencesOptionTableImageList = [NSDictionary(object: textEditingImage?, forKey: "image")]; " but still the same error
@Ql HaoYan, adding a ? at the end of an optional only unwraps it for chained events. Try var textEditingImage = NSImage(named: "NSFontPanel")!
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
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"

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.