I am trying to write a category over iTunesTrack with associated objects (an NSMutableDictionary and an NSNumber)
#import "iTunes.h"
#import <objc/runtime.h>
#interface iTunesTrack (dictionary)
- (NSMutableDictionary*) getDictionary;
- (NSNumber*) getScan;
- (BOOL)scanTrack:(NSString *)equationString;
#end
This fails:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_iTunesTrack", referenced from:
l_OBJC_$_CATEGORY_iTunesTrack_$_dictionary in iTunesTrack+dictionary.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have double checked that the Scripting Bridge framework is added and that the iTunesTrack+dictionary.m file is attached to the target. Could this be some error with combining Categories with Scripting Bridge?
Update:
If I replace iTunesTrack with SBObject, this works. I have no idea why, though.
Update 2:
This problem is reproducible:
Create new project
Add Scripting Bridge Framework and the iTunes.h header file.
Create new category of iTunesTrack with an arbitrary name
Ensure that iTunesTrack+name.h imports iTunes.h
Build
I found this page which describes using NSProxy and NSCache to store iTunesTrack objects. Would this be a better solution than trying to make a category?
Scripting Bridge is quite a mess.
The class iTunesTrack is actually called ITunesTrack under the hood.
I think they were not quite consistent with the leading lowercase i.
This is why they changed it after a while, but probably did not want to do it in the header file, to not change their API.
I don't think there is a way to fix this.
You can, however, just create the category on SBObject.
// The interface can be declared as iTunesTrack
#interface iTunesTrack (Additions)
...
#end
// The category MUST be implemented on SBObject
#implementation SBObject (Additions)
...
#end
Caution
Be aware that the category will be available on every SBObject, so make sure that all properties and methods have a unique interface.
You can't put a category on iTunesTrack (or ITunesTrack, or whatever it's called in your header) because that requires the class to exist at link time, and it doesn't: Scripting Bridge creates the target application's classes dynamically at runtime. (And they have technically arbitrary names, which is why -classForScriptingClass exists.) It's still possible to add a method to the generated class, but it means mucking about with the runtime, which is generally more trouble than it's worth. Just put your category method on SBObject and try to be careful.
NSAddict's answer pointed the way to do something I've always wanted: implementing debugQuickLookObject for SBObject types to make debugging easier. Here's the category that does iTunesTrack and iTunesArtwork.
// SBObject+Extensions.h
#import ScriptingBridge;
#interface SBObject (Extensions)
- (id)debugQuickLookObject;
#end
// SBObject+Extensions.m
#import "iTunes.h"
#implementation SBObject (Extensions)
- (id)debugQuickLookObject
{
NSString *className = self.className;
if ([className isEqualToString:#"ITunesTrack"])
{
return [self handleITunesTrack];
}
else if ([className isEqualToString:#"ITunesArtwork"])
{
return [self handleITunesArtwork];
}
return [self description];
}
- (NSString*)handleITunesTrack
{
iTunesTrack *track = (iTunesTrack *)self;
NSMutableString *s = [NSMutableString new];
[s appendFormat:#"Title: %#\n", track.name];
[s appendFormat:#"Artist: %#\n", track.artist];
[s appendFormat:#"Album: %#\n", track.album];
[s appendFormat:#"Duration: %f seconds\n", track.duration];
return s;
}
- (NSImage*)handleITunesArtwork
{
iTunesArtwork *artwork = (iTunesArtwork *)self;
NSData *data = [artwork rawData];
NSImage *image = [[NSImage alloc] initWithData:data];
return image;
}
#end
You may find the answer in this discussion:
linker command failed with exit code 1 (use -v to see invocation)
Also you can try to clean and then rebuild your project or go to Project -> Build Settings -> Valid Architectures and check whether there's all correct. Some of these advises may help you.
Related
Following problem: I receive an Object from a framework (which is not instantiable) and I want to extend it. When I make a category, the problem is, that it doesn't have an effect on the existing object.
I thought of isa swizzling. So let the isa field point to the extended "list of selectors". But that doesn't seem to be possible? (Syntax for it?)
Does anyone know a better approach to do it ?
That is the code:
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests {
//want to do something that uses the extension
}
And I want to extend CBATTRequest. I think the problem lies in CoreBluetooth?
This is how I make my category:
BLERequestable.h
#protocol BLERequestable <NSObject>
- (nonnull NSString *)getCentralUUID;
- (nonnull NSString *)getCharacteristicUUID;
- (nullable NSData*)getData;
- (void)setData:(nullable NSData *) data;
#end
CBATTRequest+Requestable.h
#import <CoreBluetooth/CoreBluetooth.h>
#import "BLERequestable.h"
#interface CBATTRequest (Requestable) <BLERequestable>
#end
CBATTRequest+Requestable.m
#import "CBATTRequest+Requestable.h"
#implementation CBATTRequest (Requestable)
- (NSString *)getCentralUUID {
return self.central.identifier.UUIDString;
}
- (NSString *)getCharacteristicUUID {
return self.characteristic.UUID.UUIDString;
}
- (NSData*)getData {
return self.value;
}
- (void)setData:(NSData *) data {
self.value = data;
}
#end
And I import the Category everywhere I want to use it.
After long researching and testing (thanks to Rob Napier), I found the source of error.
My project consists of a library and and executable target. In the library I defined my category and used it inside. The problem is, that when it comes to linking due to the executable, the o-file with my category isn't linked. See this stack post for further details on problems with categories in static libraries.
One possible solution would be setting the linker flag from the exe target to -Objc.
But I don't like this solution, because the library's ability to work properly would depend on the exe.
So I included the implementation of the category in the m-file where I use it.
If somebody has another (better) solution, I would be glad to see it. Otherwise I would close this question.
Does Objective-C have an equivalent to java annotations?
What's I'm trying to do is create a property and be able to somehow access some metadata about it.
I want to be able to determine what type of classes should go in my array so I'd like to annotate it somehow to say so. Then later be able to access that annotation via something like the runtime library where I can access lists of properties and their names.
//Put some sort of annotation giving a class name.
#property (strong) NSArray *myArray;
You said:
I want to be able to determine what type of classes should go in my array so I'd like to annotate it somehow to say so. Then later be able to access that annotation via something like the runtime library where I can access lists of properties and their names.
There are a few ways to do this sort of thing in Objective-C. Apple's frameworks do this sort of thing by adding a class method that returns the required information. Examples: dependent keys in KVO, +[CALayer needsDisplayForKey:] and related methods.
So, let's create a class method that returns an array of classes that can go into your container property, given the property name. First, we'll add a category to NSObject to implement a generic version of the method:
#interface NSObject (allowedClassesForContainerProperty)
+ (NSArray *)allowedClassesForContainerPropertyWithName:(NSString *)name;
#end
#implementation NSObject (allowedClassesForContainerProperty)
+ (NSArray *)allowedClassesForContainerPropertyWithName:(NSString *)name {
if (class_getProperty(self, name.UTF8String)) {
return #[ [NSObject class] ];
} else {
[NSException raise:NSInvalidArgumentException
format:#"%s called for non-existent property %#", __func__, name];
abort();
}
}
#end
As you can see, this default version of the method doesn't do anything particularly useful. But adding it to NSObject means we can send the message to any class without worrying about whether that class implements the method.
To make the message return something useful, we override it in our own classes. For example:
#implementation MyViewController
+ (NSArray *)allowedClassesForContainerPropertyWithName:(NSString *)name {
if ([name isEqualToString:#"myArray"]) {
return #[ [UIButton class], [UIImageView class] ];
} else {
return [super allowedClassesForContainerPropertyWithName:name];
}
}
...
We can use it like this:
SomeViewController *vc = ...;
SomeObject *object = ...;
if ([[vc.class allowedClassesForContainerPropertyWithName:#"bucket"] containsObject:object.class]) {
[vc.bucket addObject:object];
} else {
// oops, not supposed to put object in vc.bucket
}
There is no native support of this functionality, but you may to take a look at following solution — https://github.com/epam/lib-obj-c-attr/ It is compile time implementation of attributes. Definition of attributes based on defines but not on comments as in other solutions like ObjectiveCAnnotate.
Objective C does not support generics like in Java but ofcourse the language is very flexible that you can accomplish almost anything with simple tricks and knowledge. To implement a generic like feature you could create a category on NSArray class and create your own method to initialize the array and then check to see if the object is really the type of the object you want.
I would write a simple category on NSArray to have such functionality. Say suppose, I want my array to hold objects of class MyClass only then my category would look like,
#interface NSArray(MyCategory)
#end
#implementation NSArray(MyCategory)
-(NSArray*)arrayWithMyClasses:(NSArray*)classes{
if([classes count] > 0){
NSMutableArray *array = [[NSMutableArray alloc] init];
for(id anObj in classes){
NSAssert([anObj isKindOfClass:[MyClass class]], #"My array supports only objetcts of type MyClass");
[array addObject:anObj];
}
return array;
}
return nil;
}
#end
Of course, there is some limitations to it. Since you have created your own category, you should use your own method to initialize and create your own array.
No, Objective-C has no annotation or generics support.
A way to implement such a thing would be to hack Clang to read comments and associate a metadata object to the original object. But, you would be tied to your hacked compiler.
NSString *v1 = [[NSString alloc] init];
// associate
static char key;
NSString *v2 = [[NSString alloc] init];
objc_setAssociatedObject (
v1,
&key,
v2,
OBJC_ASSOCIATION_RETAIN
);
// retrieve
NSString *associate = (NSString *)objc_getAssociatedObject(v1, &key);
Qualifying with a protocol wouldn't be much trouble, and you could test if the collection implements it, but along the way you would need to create a category for each type on the same collection. This would require a different collection at compile time using macros. Overly complicated.
#interface Tomato:NSObject #end
#implementation Tomato #end
#protocol TomatoNSArray <NSObject>
- (Tomato*)objectAtIndexedSubscript:(NSUInteger)index;
- (void)setObject:(Tomato*)tomato atIndexedSubscript:(NSUInteger)index;
#end
// here is the problem, you would need to create one of this for each type
#interface NSMutableArray (TomatoNSArray) <TomatoNSArray>
#end
int main(int argc, char *argv[]) {
#autoreleasepool {
NSMutableArray<TomatoNSArray> *tomatoes = [[NSMutableArray alloc] initWithCapacity:2];
tomatoes[0] = [Tomato new];
tomatoes[1] = [NSObject new]; // warning: incompatible pointer types
}
}
Does Objective-C have an equivalent to java annotations?
Not exactly an equivalent, but there is, and it's better. In Objective-C, the compiler has to store some type and name information in the compiled code (because the language is highly dynamic, a lot of things happen at runtime as opposed to compile time), for example method names ("selectors"), method type signatures, data about properties, protocols, etc. The Objective-C runtime library then has access to this data. For example, you can get the list of properties an object has by writing
id object = // obtain an object somehow
unsigned count;
objc_property_t *props = class_copyPropertyList([object class], &count);
Or you can check what class an object belongs to:
if ([object isKindOfClass:[NSArray class]]) {
// do stuff
}
(Yes, part of the runtime library is itself wrapped into some methods of NSObject for convenience, others only have C function APIs.)
If you specifically want to store custom metadata about an object or a class, you can do that using associated references.
I expect it should be clear now, the answer is NO, not at the moment.
Some people found some alternatives which seem to work in their specific use cases.
But in general there is no comparable feature yet in objective-c. IMHO clang metadata seems to provide a good foundations for this, but as long as there is not support from Apple this will not help, as far as i understood it.
Btw. I guess it should be clear, but just to repeat for all: two changes are required to support annotations as provided in java.
The language need an extension the annotate e.g. methodes, properites, classes, ... in the source code.
A standard interface is required to access the annotated information. This can only provide by apple.
Most alternativ soltuions move the annotation information into runtime and define their own interface. The objective-c runtime provide a standard interface but only with some trick you can annotate properties and still the isse of runtime population.
The typical use case for suche a feature is an IOC container (in Java e.g. Spring) which use the annotated information to inject other objects.
I would suggest to open an feature requrest for Apple to support this.
The answer to your question is that Objective-C does not have a direct equivalent of annotations as found in Java/C#, and though as some have suggested you might be able to engineer something along the same lines it probably is either far too much work or won't pass muster.
To address your particular need see this answer which shows how to construct an array which holds objects of only one type; enforcement is dynamic and not static as with parametric types/generics, but that is what you'd be getting with your annotation so it probably matches your particular need in this case. HTH.
What you need maybe a metadata parser for Objective-C. I have used ObjectiveCAnnotate (compile time retrievable) and ROAnnotation(runtime retrievable).
Having got to grips a bit with the ParseKit grammar syntax (playing around in the demo app) I'm now trying to get my own mini demo working, but so far without much success. The assembler callbacks are not getting called.
Below is a condensed version of the relevant code. When testParse runs the parser seems to do it's thing OK and correctly match my string to my anything production (which also works in the demo) but didMatchAnything: is just not getting called.
#import <Foundation/Foundation.h>
#class PKParser;
#interface FileParserThing : NSObject {
PKParser* _parser;
}
- (void)testParse;
#end
#import <ParseKit/ParseKit.h>
#import "FileParserThing.h"
#interface FileParserThing ()
#property (nonatomic, retain)PKParser* parser;
- (void)didMatchAnything:(PKAssembly *)a;
#end
#implementation FileParserThing
#synthesize parser = _parser;
-(id)init
{
if (!(self = [super init])) return nil;
NSString *g = #"#start = anything; anything = Any+;";
self.parser = [[PKParserFactory factory] parserFromGrammar:g assembler:self];
return self;
}
- (void)testParse
{
NSString *s = #"Foo Bar";
NSLog(#"test parse with: %#", s);
[self.parser parse:s];
}
- (void)didMatchAnything:(PKAssembly *)a
{
NSLog(#"Hooray!");
}
#end
Digging around in the ParseKit code I can see that line 129 of PKParser
[assembler performSelector:assemblerSelector withObject:self withObject:a];
Isn't being executed, because assembler is nil. Which, in turn, leads me to the parser factory; where my understanding of what's going on begins to fail.
Disclaimer; I know, I probably need to read The Book, but one thing at a time. I want to get a small proof of concept working, before forking out 30 mice for a book I might never read again if my project is a non-starter :)
Developer of ParseKit here.
A while back I changed the signature of the Assembler callbacks to accept two arguments:
The Parser which matched the current token.
The Assembly containing the current state of the input parsing.
Previously, there had only been one argument: The Assembly.
I'm not sure the docs are fully updated to reflect this.
So I suspect that if you simply change your Assembler callback method to this, it will work:
- (void)parser:(PKParser *)p didMatchAnything:(PKAssembly *)a {
NSLog(#"%s %#", __PRETTY_FUNCTION__, a);
}
If not, let me know, and I'll help to further debug.
For background: I made this change because I ran into a situation where my Assembler callback really needed to inspect the Parser which had just made the current match.
It also aligned more closely the strong Cocoa convention of Delegate callbacks which always have the delegator object as their first argument. In hindsight I kinda wish I had renamed the whole concept of Assemblers in ParseKit to Delegates. Since in Cocoa parlance, that's basically what Assemblers are.
I am just learning how to use ScriptingBridges. I made a method that slowly fades the volume on iTunes, and would like to make it a category so I can do the following:
iTunesApplication* iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
[iTunes lowerVolume:50 speed:1];
I made another category for NSSpeechSynthesizer that works, but I can't get this one to. I keep getting the following build error:
"_OBJC_CLASS_$_iTunesApplication", referenced from:
l_OBJC_$_CATEGORY_iTunesApplication_$_iTunesApplicationAdditions in iTunesApplication.o
objc-class-ref-to-iTunesApplication in iTunesApplication.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Is there something special I can do to make it work since I can't include the symbols?
Thanks,
Ryan Pendleton
UPDATE:
I only found one solution, which is below. It involves MethodSwizzling, so I'm open to better answers, but for now it's all I have.
The solution I found was to use the Objective-C runtime API. I'm sure there's a better way to organize this, but here's how I did it:
Here are my .h and .m files for creating the category. Notice how lowerVolume is not an actual method, but a C function with the arguments id self, and SEL _CMD. You'll also notice a setupCategories function. We'll call that later.
// iTunes+Volume.h
#import <objc/runtime.h>
#import "iTunes.h"
void lowerVolume(id self, SEL _cmd, int dest, float speed);
void setupCategories();
#interface iTunesApplication (Volume)
- (void)lowerVolume:(int)dest speed:(float)speed;
#end
// iTunes+Volume.m
#import "iTunes+Volume.h"
void lowerVolume(id self, SEL _cmd, int dest, float speed)
{
NSLog(#"Lower Volume: %i, %f", dest, speed);
}
void setupCategories()
{
id object = [[SBApplication alloc] initWithBundleIdentifier:#"com.apple.iTunes"];
Class class = [object class];
[object release];
class_addMethod(class, #selector(lowerVolume:speed:), (IMP)lowerVolume, "#:if");
}
Now that I've made the functions, I need to actually add them to the scripting bridge class using the Objective-C runtime API. I'll do this in main.m to make sure that the methods are ready to be used when the run loop starts.
// main.m
#import <Cocoa/Cocoa.h>
#import "iTunes+Volume.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
setupCategories();
return NSApplicationMain(argc, (const char **) argv);
[pool drain];
}
Now, I can use my method wherever I want as long as I include the header files:
- (void)mute
{
iTunesApplication* iTunes = [[SBApplication alloc] initWithBundleIdentifier:#"com.apple.iTunes"];
[iTunes lowerVolume:0 speed:1];
[iTunes release];
}
If any of this doesn't make sense, just tell me and I'll try to explain it better.
I think you need to include -framework ScriptingBridge to your gcc arguments. That got it to compile for me!
As noted above, you can't easily do a category on iTunesApplication because it doesn't exist at compile time, and also because the runtime class name is ITunesApplication (capital "I").
The best solution I've found is to do your category on the class that DOES exist, SBApplication. Here's the code I tested that works and does what the original example was trying to do:
// SBApplication+Extensions.h
#import ScriptingBridge;
#interface SBApplication (Extensions)
- (void)lowerVolume:(int)dest speed:(float)speed;
#end
// SBApplication+Extensions.m
#import "iTunes.h"
#import "SBApplication+Extensions.h"
#implementation SBApplication (Extensions)
- (void)lowerVolume:(int)dest speed:(float)speed
{
NSLog(#"Lower Volume: %i, %f", dest, speed);
}
#end
// Caller, say in AppDelegate
#import "SBApplication+Extensions.h"
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
iTunesApplication *iTunesApp =
[SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
[iTunesApp lowerVolume:4 speed:3.3f];
}
Here's an odd one. I have a class named TileMap with the following interface:
#interface TileMap : NSObject
{
int *data;
int tilesWide;
int tilesHigh;
NSString *imageName;
}
+ (id)mapNamed:(NSString *)filename;
- (id)initWithFile:(NSString *)filename;
#end
The implementation looks like this:
#implementation TileMap
+ (id)mapNamed:(NSString *)filename
{
return [[self alloc] initWithFile:filename];
}
- (id)initWithFile:(NSString *)filename
{
if (self = [super init])
{
// ...
}
return self;
}
#end
But when I add a call to [TileMap mapNamed:#"map.plist"]; to my application Xcode warns:
'TileMap' may not respond to '+mapNamed:'
The application compiles fine and calls to NSLog within TileMap-initWithFile: are logged. I noticed that Xcode's syntax coloring was off for this class and method so I tried renaming both the class and the method separately. The only combination that eliminated the warning and syntax coloring issues was to rename both the class and the method.
Am I colliding with some undocumented framework class or method? Find in Documentation doesn't reveal anything. Find in Project only reveals the call, interface definition and the implementation. I'm stumped (not that it takes much). Is there a way around this without munging my existing naming conventions?
Did you #import the TileMap.h header? Did you save your TileMap.h header?
Turns out my project directory ended up with two TileMap.h and TileMap.m files—visible from the Finder but not in Xcode. One, a complete interface and implementation, in my root project directory. The other just a bare NSObject subclass in my framework subdirectory. Not sure how that happened. Deleting the latter resolved the problem. Thanks for the help just the same Dave.
Shaun,
besides the problem you asked about, you also have a memory leak in +mapNamed:. The following line returns a non-autoreleased object with a retain count of +1, which basically gives ownership to the caller:
return [[self alloc] initWithFile:filename];
According to the Memory Management Programming Guide for Cocoa, you should return autoreleased objects from convenience methods, such as this:
return [[[self alloc] initWithFile:filename] autorelease];
If you have Snow Leopard and Xcode 3.2, you might want to try running the static analyzer to find mistakes such as this one by pressing Cmd+Shift+A.