顯示包含「Cocoa」標籤的文章。顯示所有文章
顯示包含「Cocoa」標籤的文章。顯示所有文章

2014年6月22日星期日

Parse framework on Cocoa (OSX)

Got this error when running the empty project after imported Parse SDK.

"dyld: Library not loaded: @executable_path/../Frameworks/ParseOSX.framework/ParseOSX Referenced from: /Users.......Reason: image not found"

And here is the answer: https://parse.com/questions/parse-framework-not-working-in-cocoa


  1. Add a Copy Files Build Phase (Editor -> Add Build Phase -> Add Copy Files Build Phase)
  2. Then set the destination to Products Directory.
  3. Then click on the + button to choose the Parse Framework.
Seems a useful technique to deal with missing library or linking problem.

2010年12月19日星期日

Cocoa runtime working directory

To specify a working directory of the application at runtime, we could use the 
changeCurrentDirectoryPath:(NSString*) of the NSFileManager

where the [NSBundle mainBundle] get the mainBundle and in my case I need the  "resourcePath" to get the path containing my stuffs.

2010年12月8日星期三

Export NSView to PNG

The following code is for exporting a NSView to PNG file.


And you can change the NSPNGFileType for other format, of course.

KVC for BOOL type not working

Recently I tried to read xml files by KVC in order to get rid of the frustrating hard-coded name matching.

However, by using the  -setValue:forKeyPath: function, I got a "-[NSCFString charValue]: unrecognized selector sent to instance XXXXXX" error when it comes to setting value of any property of BOOL type.

I could only found a few reference on Stackoverflow.

@try
    {
        [(NSObject*)retObj setValue:[[obj keyValuePairs] objectForKey:key]
                         forKeyPath:key];
    }
    @catch (NSException * e)
    {
        if ([[e name] isEqualToString:NSInvalidArgumentException])
        {
            NSNumber* boolVal = [NSNumber numberWithBool:[[[obj keyValuePairs] objectForKey:key] boolValue]];
            [(NSObject*)retObj setValue:boolVal
                             forKeyPath:key];
        }
    }

Since I am not knowing the type until runtime, I need to catch the exception before converting the data type. But I am not sure whether any other type would cause the same exception and lead to an incorrect casting.

I haven't met any problem yet, and hopefully it is safe

2010年12月2日星期四

NSImage Transformation - Flip

Today a lot of time was wasted on deal with image transformation due to lack of investigation about the properties of drawing in Cocoa.

I can't make the transform context effect only on the image I want to transform but the whole NSView owning the image. After using "lockfocus", nothing is drawn becox of the wrong implementation of the coordination system.
The pain ended after I kw about the difference between "bounds" and "frame" on Stackoverflow

To make the "lock" function, it seems that the "frame" need to be set correctly and after the "lock" the drawing is easy.

Here is a simple sample of flipping a NSImage with the NSAffineTransform class. The image is drawn from its middle bottom.

2010年11月22日星期一

Few little technique on Objective-C

To test an object's type in runtime in Obj-C:

[myObject isKindOfClass:[NSString class]]

To get the mouse position on the screen:

NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation];


To get the mouse position wrt local window:

NSPoint  point   = [self convertPoint:[theEvent locationInWindow] fromView:nil];

2010年11月17日星期三

Cocoa NSCollectionView

Just found a useful tutorial at André's Blog when making the map editor for the new project(we are tired at making the map xml files manually).

Even it is "for dummies", it takes me some time to follow as a newcomer to Cocoa and Mac OS. The version difference even make it worser as something has changed in Xcode.

tbc