How to write really dynamic methods in Objective-C?

160 Views Asked by At

I have just started to develop this library

It's main goal is to allow programmer to write methods name like sentences to work with Core Data.

Examples:

[moc RD_createUserWithName:@"John" age:@29 married:@YES];
[moc RD_createCompanyWithName:@"Yandex, LLC" ceo:me managers:@[firstManager, secondManager]];
[moc RD_createCompanyWithName:@"Google" ceos:@[larryPage, sergeyBrin] manager:jonathanGreen];
[moc RD_createBookWithAuthor:joanneRowling name:@"Harry Potter" publishedAt:[NSDate date]];
[moc RD_createTaskForProject:googleTalk priority:@3 members:nil];
[moc RD_createArticleAboutUser:alizar withName:@"Alizar - Habrahabr Soul" andTitle:@"Alizar"];

or finding objects:

NSArray* mos = [moc RD_findUsersWithFirstName:@"Igori"];
NSManagedObject* mo = [moc RD_findUserWithLastName:@"Guliko" firstName:@"Igori"];
NSArray* mos = [moc RD_findBooksPublishedAt:[NSDate date]];
NSArray* mos = [moc RD_findFriendsLastSeenAt:[NSDate date]];
NSArray* mos = [moc RD_findBooksPublishedAfter:[NSDate date]];
NSArray* mos = [moc RD_findBooksPublishedBefore:[NSDate date]];
NSManagedObject* mo = [moc RD_findStudentCreatedBefore:[NSDate date] emailLike:@"*@gmail.com"];
NSArray* mos = [moc RD_findCompaniesRegisteredAfter:[NSDate date] limit:@10 offset:@1];
NSArray* mos = [moc RD_findClientsAddedBetween:@[startDate, endDate]];
NSArray* mos = [moc RD_findStudentsWithAgeBetween:@[@18, @28]]; // plural
NSManagedObject *mo = [moc RD_findStudentWithAgeBetween:@[@18, @28]]; // singular
NSArray* mos = [moc RD_findUsersWithGender:@[@"Male", @"Female"] lastNameLike:@"*ir*"  ageBetween:@[@18, @29]];
NSArray* mos = [moc RD_findCarsWithFuelVolumeGreaterThan:@10];
NSArray* mos = [moc RD_findMessagesWithPositiveVotesSmallerThan:@29];

But I can not get it to work without turning ARC off. (Previous Q: Error compiling with ARC when runtime programming dynamic method ).

From mailing list:

Our reasoning was split about 50/50 between (1) needing to be more careful about types and ownership and (2) wanting to eliminate an embarrassing wart in the language (not being allowed to complain about completely unknown methods with anything more strenuous than a warning). There is really no legitimate reason to call a method that's not even declared somewhere. The ability to do this makes some really trivial bugs (e.g. typos in selectors) runtime failures instead of compile failures. We have always warned about it. Fix your code.

So, Objective-C is no more real Objective-C with dynamic methods?

Is it possible now to implement what I wanted in RubyDavidson or no?

1

There are 1 best solutions below

2
Amin Negm-Awad On

Using dynamic methods you have to solve two problems: The existence at run time, which seems to be solved and the existence for the compiler. "No" is the wrong answer to your question, because you can solve that problem.

There are two techniques to do so:

A. Put them in a category of the receiver's class. Then you have a declaration. Use it. This is called an informal protocol. It is used in Cocoa from the very beginning.

B. Put them in a protocol and make it optional. Then you have a declaration. Use it. This is called a formal protocol.