Monday, October 22, 2018

#1 2018-06-08 10:39:05 am

jcwelch
Member
Registered: 2009-09-11
Posts: 50

NSMutableArray suddenly isn't???

Okay, so this literally was working, and now it is not.

on application start, I read in an array of defaults into an NSMutableArray (because I may need to add things on to it later):

Applescript:

on applicationWillFinishLaunching_(aNotification)
       set theDefaults to current application's NSUserDefaults's standardUserDefaults()
       theDefaults's registerDefaults:{serverSettingsList:""}
       set my theSettingsList to current application's NSMutableArray's arrayWithCapacity:1                        
       set my theSettingsList to theDefaults's arrayForKey:"serverSettingsList"
my loadServerTable:(missing value)
end applicationWillFinishLaunching_

this all works correctly, as does the loadServerTable: function -

Applescript:

on loadServerTable:sender
       repeat with x from 1 to count of my theSettingsList
           set theItem to item x of my theSettingsList as record
           set the end of my theServerList to {theTableServerName:serverName of theItem,theTableServerURL:serverURL of theItem,theTableServerAPIKey:serverAPIKey of theItem}
       end repeat
       my theServerTableController's removeObjects:(theServerTableController's arrangedObjects())
       my theServerTableController's addObjects:my theServerList
       set theServerList to {} --blank out the list
   end loadServerTable:

on launch, this all runs rather nicely, bob's your uncle. But then I try to add a new server on to things with saveSettings: -

Applescript:

on saveSettings:sender
       set thePrefsRecord to {serverName:my theServerName,serverURL:my theServerURL,serverAPIKey:my theServerAPIKey}
       current application's NSLog("thePrefsRecord: %@", thePrefsRecord)
       current application's NSLog("theSettingsList: %@", theSettingsList)
       my theSettingsList's addObject:thePrefsRecord
       theDefaults's setObject:my theSettingsList forKey:"serverSettingsList"
       my loadServerTable:(missing value) --reload table with new data
end saveSettings:

when it gets to the addObject: method, which should just shove the new record onto the end of theSettingsList, I get:

[AppDelegate saveSettings:]: -[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object (error -10000)



which makes no sense to me since I explicitly declared theSettingsList as NSMutableArray. The addobject feature worked at one point, it's how I went from one to two objects in the array early on just to test writing new data into the prefs, but now it's not?

i'm completely unsure as to what is going on here.

FYI, the data for thePrefsRecord in my test:

thePrefsRecord: {
    serverAPIKey = adfasdf;
    serverName = test3;
    serverURL = "https://test3.com";
}



and the existing value of theSettingsList:

theSettingsList: (
        {
        serverAPIKey = aasodinadf;
        serverName = test1;
        serverURL = "http://test1.com";
    },
        {
        serverAPIKey = 24573245;
        serverName = test2;
        serverURL = "http://test2.com";
    }
)

Offline

 

#2 2018-06-08 12:14:43 pm

jcwelch
Member
Registered: 2009-09-11
Posts: 50

Re: NSMutableArray suddenly isn't???

Figured it out with some help from twitter. The initial read from NSDefaults was coercing the NSMutableArray to an NSArray. So shoving the initial read into a temp array then shoving THAT into NSMutableArray via addObjectsFromArray: did the trick.

sigh.

Offline

 

#3 2018-06-08 05:33:35 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5454

Re: NSMutableArray suddenly isn't???

Easier to make a mutable copy:

Applescript:

set my theSettingsList to (theDefaults's arrayForKey:"serverSettingsList")'s mutableCopy()


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2018-06-11 06:31:03 pm

jcwelch
Member
Registered: 2009-09-11
Posts: 50

Re: NSMutableArray suddenly isn't???

It fails, with a an error I can't grok:

[AppDelegate applicationWillFinishLaunching:]: missing value doesn’t understand the “mutableCopy” message. (error -1708)

Offline

 

#5 2018-06-11 08:55:44 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5454

Re: NSMutableArray suddenly isn't???

That error means (theDefaults's arrayForKey:"serverSettingsList") is returning nil.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2018-06-14 02:14:29 pm

jcwelch
Member
Registered: 2009-09-11
Posts: 50

Re: NSMutableArray suddenly isn't???

figured it out with some help from Nebel: I was initially defining serverSettingsList as text instead of as a list/array. Changed that and it handles nils much better

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)