Singleton or class methods?
I have a project that involves several different targets. Included in all this is a set of related utility methods that need to be used by different sections of the code. I am wondering how is the best way to do this so that I don't have the same utility methods written in different places. 1. I could make a class MyUtilities that consisted of the 10 or so methods as class methods and the class would have no instance methods or instance variables. 2. I could make a singleton that put the methods as instance methods, but also had no instance variables. Thoughts? I can't really make these methods a category on anything else. These methods are used to convert between an NSScreen, CGDirectDisplayID, io_service_t, and a few other ways of dealing with screen identification. So a category on NSScreen is not going to work since in some cases an NSScreen is not known/used. Trygve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Singleton or class methods?
Why not use straight C functions? Sent from my iPad On Sep 13, 2014, at 20:45, Trygve Inda cocoa...@xericdesign.com wrote: I have a project that involves several different targets. Included in all this is a set of related utility methods that need to be used by different sections of the code. I am wondering how is the best way to do this so that I don't have the same utility methods written in different places. 1. I could make a class MyUtilities that consisted of the 10 or so methods as class methods and the class would have no instance methods or instance variables. 2. I could make a singleton that put the methods as instance methods, but also had no instance variables. Thoughts? I can't really make these methods a category on anything else. These methods are used to convert between an NSScreen, CGDirectDisplayID, io_service_t, and a few other ways of dealing with screen identification. So a category on NSScreen is not going to work since in some cases an NSScreen is not known/used. Trygve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/xcvista%40me.com This email sent to xcvi...@me.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Singleton or class methods?
On Sep 13, 2014, at 7:45 AM, Trygve Inda cocoa...@xericdesign.com wrote: These methods are used to convert between an NSScreen, CGDirectDisplayID, io_service_t, and a few other ways of dealing with screen identification. So a category on NSScreen is not going to work since in some cases an NSScreen is not known/used. A category can add class methods. Regards, Ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Singleton or class methods?
I use a singleton for variable storage, a utility class for methods I want to call from anywhere and everywhere. On Sep 13, 2014, at 8:45 AM, Trygve Inda wrote: I have a project that involves several different targets. Included in all this is a set of related utility methods that need to be used by different sections of the code. I am wondering how is the best way to do this so that I don't have the same utility methods written in different places. 1. I could make a class MyUtilities that consisted of the 10 or so methods as class methods and the class would have no instance methods or instance variables. 2. I could make a singleton that put the methods as instance methods, but also had no instance variables. Thoughts? I can't really make these methods a category on anything else. These methods are used to convert between an NSScreen, CGDirectDisplayID, io_service_t, and a few other ways of dealing with screen identification. So a category on NSScreen is not going to work since in some cases an NSScreen is not known/used. Trygve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com This email sent to z...@mac.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Singleton or class methods?
I would go with class methods. Apple's APIs tend to use instance methods (NSFileManager, NSNotificationCenter, etc.); the benefit of those is that you can have multiple instances with different state (like delegates), or different behaviors. The doesn't seem like it's relevant to your case, though, and it's nice to be able to avoid to write sharedInstance all the time. Straight C functions work too; I also use those for things like this. Pros: they're faster. Cons: the call site is less descriptive. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Loading image resources
Hi, everyone. In my project’s images.xcassets, I created three image sets with these names: Folder SinglePage MultiPage Each one contains a single PNG image. Now I’m trying to load them into NSImages. Here’s what doesn’t work: let folderImage: NSImage let singleDocumentImage: NSImage let multiDocumentImage: NSImage override init() { // Swift requires local properties to be initialized first theProject = DocumentNode.CreateSampleProject() folderImage = Document.getImageAsset( Folder ) singleDocumentImage = Document.getImageAsset( SingleDocument ) multiDocumentImage = Document.getImageAsset( MultiDocument ) super.init() } class func getImageAsset( name: String ) - NSImage { let bundle = NSBundle.mainBundle() let imageFileName = bundle.pathForResource( name, ofType: png ) let img = NSImage( contentsOfFile: imageFileName ) return img } After calling bundle.pathForResource:ofType:, imageName is nil. I think what I’m doing is what the Resource Programming Guide says to do. How am I getting it wrong? -- Charles Jenkins ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading image resources
On Sep 13, 2014, at 12:28 , Charles Jenkins cejw...@gmail.com wrote: After calling bundle.pathForResource:ofType:, imageName is nil. I think what I’m doing is what the Resource Programming Guide says to do. How am I getting it wrong? a. Yes, but did you check whether bundle or imageFileName is nil? Is imageFileName the correct path to where the files actually are? b. No one should be using path-based API any more, generally. You should use URL-based equivalents. c. IAC, there’s an easier way. For example: folderImage = NSImage.imageNamed (@“Folder”); No mucking about with paths or URLs! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: NSUserDefaults not sticking
1. Only certain ones that I know of 2. At application launch 3. No 4. I think Mavericks but not 100% certain 5. For the affected users yes 6. Not sure about this could check 7. Upon my request yes 8. Not sure would have to check On Sep 11, 2014, at 3:21 PM, Bavarious bavari...@icloud.com wrote: Em 11/09/2014, à(s) 01:07, Rick C. rickcort...@gmail.com escreveu: This is all very interesting and shows that there are issues out there, but back to my original issue if I’m writing and reading via NSUserDefaults and its not returning the expected values what else could be the trouble? 1) Do all preference values get lost or is it only a subset of them? 2) When in the application life cycle are these values stored and read? 3) Do you ever send -[NSUserDefaults synchronize]? 4) Do users that experience data loss use the same OS X version(s)? 5) Do they reproducibly lose defaults? 6) Does Console show any error messages for cfprefsd on their boxes? 7) Have those users ever removed the underlying .plist file? 8) Do those users run ‘System Optimisation/Cleanup’ utility applications? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Reset dispatch_once_t if the initialization failed?
I’ve seen bog-standard code to initialize a shared instance like: + (SomeSingleton *) sharedInstance { static SomeSingleton *sharedInstance = nil; static dispatch_once_t onceToken; static dispatch_once_t onceToken; dispatch_once(onceToken, ^{ sharedInstance = [[SomeSingleton alloc] init]; }); return sharedInstance; } What happens if that initialization fails? Shouldn’t there be a test for NIL and a reset of the “onceToken” somewhere?… (Or is the chance of failure too unlikely?) — Daryle Walker Mac, Internet, and Video Game Junkie darylew AT mac DOT com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Reset dispatch_once_t if the initialization failed?
On Sep 13, 2014, at 9:14 PM, Daryle Walker dary...@mac.com wrote: I’ve seen bog-standard code to initialize a shared instance like: + (SomeSingleton *) sharedInstance { static SomeSingleton *sharedInstance = nil; static dispatch_once_t onceToken; static dispatch_once_t onceToken; dispatch_once(onceToken, ^{ sharedInstance = [[SomeSingleton alloc] init]; }); return sharedInstance; } What happens if that initialization fails? Shouldn’t there be a test for NIL and a reset of the “onceToken” somewhere?… (Or is the chance of failure too unlikely?) Plenty of ObjC code assumes that initialization can’t fail. Sometimes, but not always, the fallibility of the initializer is documented. Thankfully Swift puts pressure on you to make that assumption explicit. Besides all that, you can’t reset a dispatch_once. The contract requires that you pass it a pointer to a dispatch_once_t that you guarantee has been initialized zero for the entire life of the program. Otherwise you’ve just moved the race condition from initializing your singleton to initializing the dispatch_once. Technically this means you can’t have a dispatch_once instance variable, as the memory at that address may have been nonzero before the object was allocated. In practice, I don’t think it’s an issue (nobody should be able to send messages to your object until initialization finishes anyway), but I invite someone from the runtime team (*cough* Greg Parker *cough*) to explain why I’m wrong. :) --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading image resources
*Sigh* I seem to make a typo every time I post to this list. Of course I meant that the imageFileName returned by the API call is nil. -- Charles Jenkins On Saturday, September 13, 2014 at 5:12 PM, Quincey Morris wrote: On Sep 13, 2014, at 12:28 , Charles Jenkins cejw...@gmail.com (mailto:cejw...@gmail.com) wrote: After calling bundle.pathForResource:ofType:, imageName is nil. I think what I’m doing is what the Resource Programming Guide says to do. How am I getting it wrong? a. Yes, but did you check whether bundle or imageFileName is nil? Is imageFileName the correct path to where the files actually are? b. No one should be using path-based API any more, generally. You should use URL-based equivalents. c. IAC, there’s an easier way. For example: folderImage = NSImage.imageNamed (@“Folder”); No mucking about with paths or URLs! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Loading image resources
On Sep 13, 2014, at 21:33 , Charles Jenkins cejw...@gmail.com wrote: Of course I meant that the imageFileName returned by the API call is nil. Ah, OK, but you still need to verify that everything is in place. For example, if you forgot to add the asset folder to the target, the image files wouldn’t be in the built app. Also, I’m not entirely certain that “png” is the correct extension for an image compiled from xcassets. (I don’t have a project handy where I can check.) Perhaps you really do need to be using different API — ‘imageNamed:’ which I mentioned before, but I also notice that NSBundle AppKit additions also have ‘imageForResource:’ and ‘URLForImageResource:’ which don’t take an extension. Perhaps they exist exactly for this situation? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com