0

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?

Community
  • 1
  • 1
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
  • `class_getInstanceMethod()`, `- [NSObject performSelector]` and others to the rescue. –  Oct 13 '13 at 06:21
  • @H2CO3, no-no) I wanted to allow programmer to write such code in his source codes, after that, when app is running this methods will be parsed in forwardInvocation: and create right entity or find right entities which were taken from method name. – AndrewShmig Oct 13 '13 at 06:24
  • And how do those methods prevent you from doing so? –  Oct 13 '13 at 06:27
  • @H2CO3, I cannot compile code with unknown selectors and ARC turned on. When I turn OFF ARC for the whole project - I can, but it is not what I really want. – AndrewShmig Oct 13 '13 at 06:32
  • Um, I see. How about using `NSSelectorFromString()`? Does the compiler catch that as well? –  Oct 13 '13 at 06:34
  • @H2CO3, with NSSelectorFromString() it works fine, but the WHOLE point of RubyDavidson is to allow DYNAMIC methods which will be parsed (entity name, action, attribute names) and processed. – AndrewShmig Oct 13 '13 at 06:38
  • 1
    With ARC, the compile *does not accept* `[foo someMethod]` if "someMethod" is not declared somewhere (in an interface or protocol). That's what I wrote in my answer to the question that you linked to, and the situation has not changed. The posting from the Apple mailing list shows that this was a deliberate decision from the compiler people. – Martin R Oct 13 '13 at 06:44
  • @MartinR, ok, so the answer is no to my Q? We can not now create really dynamic methods with ARC turned ON? And there is no way to implement what I wanted in RubyDavidson? Thx. – AndrewShmig Oct 13 '13 at 06:47
  • That's true. To call a method directly (without using performSelector, objc_msgSend, ...), that method *has to* be declared somewhere. - This question seems to be a duplicate of the one that you linked to. – Martin R Oct 13 '13 at 06:54
  • @MartinR, very sad... whats the point of cutting off such powerful features? – AndrewShmig Oct 13 '13 at 07:17
  • what you are trying to achieve is nothing more than syntaxsugar, so it is not powerful in a uniq way as you can easily do what you want with different syntax. There is no power lost in the language. There is no real feature left, you should always try to do things the way the language/Framework wants you to do not the way you are used to in a complete different environment. You could solve your problem with strings, macros etc. with the same behaviour and the same amount of code. – Jonathan Cichon Oct 13 '13 at 07:22
  • 1
    @AndrewShmig: I can't tell you more about the reasons than what was said in the Apple mailing list: 1) ARC needs to know about the memory semantics (well, actually only the memory semantics of the return value are relevant!) - 2) The ability to do this makes some really trivial bugs (e.g. typos in selectors) runtime failures instead of compile failures. – Martin R Oct 13 '13 at 07:54
  • possible duplicate of [Error compiling with ARC when runtime programming dynamic method](http://stackoverflow.com/questions/13007797/error-compiling-with-arc-when-runtime-programming-dynamic-method) – Martin R Oct 13 '13 at 13:16

1 Answers1

-1

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.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • Put them in a category? How I can do that if I dont know all possible method names which programmer can use in RubyDavidson? – AndrewShmig Oct 13 '13 at 10:28
  • Ah, you even do not know the possible methods used there? When I have such a pattern, I let the user – he definetly knows the methods, he wants to use – put the methods in a protocol and import it. – Amin Negm-Awad Oct 13 '13 at 10:30