#!/usr/bin/env objc-run @import Foundation; @implementation Hello : NSObject - (void) sayHelloTo:name { printf("Hello %s, my address is %p\n", [name UTF8String], self); } @end int main () { id hello = [Hello new]; [hello sayHelloTo:@"sunshine"]; }
brew install objc-run
brew install objc-run
and add permissions to execute your source file: chmod u+x main.m
@interface
ads I accidentally found out that ObjC allows you to specify a superclass directly in the @implementation
directive. It is not entirely clear why this was allowed, but this allows you to completely get rid of the @interface
block.-(id)doSomethingWith:(id)param
; this is exactly the same as -doSomethingWith:param
; but the second option looks more convenient.void main ()
instead of int main (int argc, char**argv)
. Why declare it all, if you still do not use these arguments?return
in main()
. Starting from the C99 standard, when returning control from main()
without a return statement, it is considered that return 0;
was called return 0;
@interface
warning warning annoys me:
/dev/fd/63:3:17: warning: cannot find interface declaration for 'Hello' @implementation Hello : NSObject ^ 1 warning generated.
warn_undef_interface
for which there is no corresponding -W flag (to silence warnings by type). So for myself, I left an empty interface.
#!/usr/bin/env objc-run @import Foundation; @interface Hello : NSObject @end @implementation Hello - (void) sayHelloTo:name { printf("Hello %s, my address is %p\n", [name UTF8String], self); } @end int main () { id hello = [Hello new]; [hello sayHelloTo:@"sunshine"]; }
Source: https://habr.com/ru/post/242621/