Archive

Archive for the ‘Objective C’ Category

Making NSAssert work

July 12th, 2011 No comments

Hi,

NSAssert is handy but it has been annoying me recently as the execution of the debugger doesn’t stop when an assert is hit. Instead we get a message output to the console and the execution is stopped in the update loop at a later point. When something hits an assertion I want the program execution to stop immediately. For some reason beyond me this is disabled in XCode by default. To enable it go to Run->Stop on Objective-C Exceptions

There you go, your exceptions will now work.

Categories: iPad, iPhone, iTouch, Objective C Tags:

Hundreds of errors including: Expected declaration specifiers or ‘…’ before ‘CFXMLTreeRef’

January 5th, 2011 1 comment

Hi,

I upgraded recently to XCode 4.2 and on building the release version of my app was getting 96 odd errors. All of these orginate from Foundation.h with a callstack like below

/Users/parrotbait/Documents/Dropbox/
/myApp/myApp_Prefix.pch

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:81:0 In file included from /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSURLError.h:17:0 In file included from /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSURLError.h

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:29:0 In file included from /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/CoreServices.framework/Frameworks/OSServices.framework/Headers/OSServices.h:54:0 In file included from /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/CoreServices.framework/Frameworks/OSServices.framework/Headers/OSServices.h

/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/CoreServices.framework/Frameworks/OSServices.framework/Headers/WSMethodInvocation.h:773:0 Expected declaration specifiers or '...' before 'CFXMLTreeRef' in /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/CoreServices.framework/Frameworks/OSServices.framework/Headers/WSMethodInvocation.h

Why were the OSX frameworks being used? As they were being used for an iOs build there was all sorts of problems. So I began to hunt down the origin of the call to the OSX headers.
After checking my project settings everything looked fine and I couldn’t see what the problem was. However after opening the transcript of the build I saw that on compiling the precompiled headers I was the errors were occuring. There was a compiler flag ‘-F’ for framework that indicates what paths to search for frameworks:

-F/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks

Right clicking on my Target and searching for Framework in the build section, I found that I had somehow (although I don’t remember doing this and am not convinced I really did) had an entry for ‘Framework Search Paths’

"$(DEVELOPER_DIR)/SDKs/MacOSX10.6.sdk/System/Library/Frameworks"

Removing this got rid of my 100s of build errors.

Categories: iPhone, iTouch, Objective C Tags:

Clear iPhone simulator cache (out-dated files being loaded)

March 15th, 2010 No comments

Quick post that might help someone.

I had an XML file that I had changed in Dashcode and for some reason the older XML content was being read into my app. I ‘re-touched’ it in XCode and even though the right XML showed up within the project the wrong XML was being loaded up in the simulator.

A little perplexed by this I checked what file was being loaded by stepping through code and saw that the XML was being loaded up from a path that looked something like “/Users//Library/Application Support/iPhone Simulator/User/Applications/xxx”. This location obviously contained the old version of the XML file and not the new one I had in XCode. To solve this problem I cleaned the project and performed a full re-build and low-and-behold the correct XML was loaded. I suspect cleaning the project deletes the app and all associated data from the simulator cache.

Categories: iPhone, Objective C Tags:

Create a class from a string

March 15th, 2010 No comments

I found myself in the position that I had a string that came in from XML and wanted to create an appropriate class from this string. After some searching I found the neat little function NSClassFromString. So given a string I could allocate a class by doing

id newClass = [[NSClassFromString(string) alloc] init];

I also needed to go back the other way, get the name of the class as a string. Again there is a similar function definition

NSString* name = NSStringFromClass([self class]);

This above shows how to get the class from self using [self class].

Categories: Objective C Tags:

Selectors with multiple arguments

March 15th, 2010 No comments

I’ve needed to figure out if a certain function exists for a class before instantiating a new class. However the init function takes two parameters and all code samples I’ve seen use selectors with just a single parameter. Below is the code that checks if a selector with two parameters is present.

if([NSClassFromString(classType) instancesRespondToSelector: @selector(initWithTwoParameters: withSecondParameter:)])
{
}
Categories: Objective C Tags: