Visualizzazione post con etichetta objectivec. Mostra tutti i post
Visualizzazione post con etichetta objectivec. Mostra tutti i post

venerdì 23 giugno 2017

Fix the issue "Unknown class xxx in Interface Builder file"

Trying to mix Objective C and Swift class I faced the followed issue: running my application and opening a specific View Controller (written in objective C) using a table cell (written in Swift) including a custom view (written in Swift) I saw the following message in the XCode 9 console:

"Unknown class MTMCheckbox in Interface Builder file"

The 3 involved class where:

MTMMatchDetailViewController (ObjC)
MTMAvailabilityCell (Swift file + XIB) 
MTMCheckbox (Swift file)

The error comes from MTMAvailabilityCell XIB.

This was the class definition for my MTMCheckbox.swift:


Thanks to this StackOverflow thread and the user1760527's answer I was able to fix adding (MTMCheckbox) after the notation @obj:

This seems a workaround renaming a Swift class: in my case, MTMCheckbox was previously an Objective C class that I rewritten in Swift.

I hope this can help someone else having the same issue.




venerdì 14 marzo 2014

@synthetize vs @dynamic in ObjectiveC

Since it's the second or third time that I find myself looking for a response to this question on Internet, I'd like to note it here and share with everyone who needs an explication of the difference between the to directives @dynamic and @syntethize in Objective C.

The response is posted by Alex Rozanski in a Stackoverflow thread

Some accessors are created dynamically at runtime, such as certain ones used in CoreData's NSManagedObject class. If you want to declare and use properties for these cases, but want to avoid warnings about methods missing at compile time, you can use the @dynamic directive instead of @synthesize.
...
Using the @dynamic directive essentially tells the compiler "don't worry about it, a method is on the way."
The @synthesize directive, on the other hand, generates the accessor methods for you at compile time (although as noted in the "Mixing Synthesized and Custom Accessors" section it is flexible and does not generate methods for you if either are implemented).

giovedì 13 marzo 2014

ObjectiveC: Create a thread safe singleton

If you need to create a singleton keeping it thread-safe, Apple recommends to use the dispatch_once paradigm.

Below an example taken from a Stackoverflow thread (where a user mention this method is 2 times faster than using the classic synchronized paradigm):

+ (MyClass *)sharedInstance
{
    //  Static local predicate must be initialized to 0
    static MyClass *sharedInstance = nil;
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[MyClass alloc] init];
        // Do any other initialisation stuff here
    });
    return sharedInstance;
}