How to #import <NSObjCRuntime.h> to use objc_msgSend - objective-c

I'd like to import runtime's header to use objc_msgSend but I'm getting:
error: NSObjCRuntime.h: No such file or directory
Should I add something to the header search path?

You need to include <objc/message.h> (you'll find the related headers in /usr/include/objc) and link to the objc (/usr/lib/libobjc.dylib) library.

#import <Foundation/NSObjCRuntime.h> does work
but you probably need
#import <objc/runtime.h>
like this Apple example does
upd: since iOS 7 #import <Foundation/NSObjCRuntime.h> replaced to #import <objc/NSObjCRuntime.h> but i recommend to use #import <objc/runtime.h> anyway

When using Xcode 6 and later, you will get an error after #include<objc/message.h>. It can be resolved like this
#include <objc/message.h>
void foo(void *object) {
typedef void (*send_type)(id, SEL, int);
send_type func = (send_type)objc_msgSend;
func(object, sel_getUid("foo:"), 5);
}
http://devstreaming.apple.com/videos/wwdc/2014/417xx2zsyyp8zcs/417/417_whats_new_in_llvm.pdf

Related

Strange issue 'Cannot find type 'RCTResponseSenderBlock' in scope' on Swift module React Native

I have a very strange problem: "Can't find the type RCTResponseSenderBlock in scope." It's strange, because in one case it is there and in the other it doesn't, looks like a bug.
This is screenshot issue.
This my Objective-C file:
#import <Foundation/Foundation.h>
#import "React/RCTBridgeModule.h"
#import <React/RCTEventEmitter.h>
#import <React/RCTConvert.h>
#interface RCT_EXTERN_MODULE(BLEclass, NSObject)
RCT_EXTERN_METHOD(addEvent:(NSString *)name callback:(RCTResponseSenderBlock)callback )
#end
And this my Swift file, where i have issue :
import Foundation
#objc(BLEclass)
class BLEclass: NSObject{
#objc(addEvent:callback:)
func addEvent(_ name: String,_ callback:RCTResponseSenderBlock){
NSLog("%#", name);
let resultsDict = [
"name" : name
];
callback([NSNull(),resultsDict])
}
}
If you know how to fix it, please write answer.
Thanks.
I had this same issue and discovered I had to add the import to my main bridge file.
#import <React/RCTBridgeModule.h>
#import <React/RCTViewManager.h>
I out which file was set by looking at the Build settings under Swift Compiler - General -> Objective-C Bridging Header which appears to be the default for the app.

Returning CGPoint from a Method in Objective-C

I have the following code:
#import <Foundation/Foundation.h>
#interface BBUtils : NSObject
{
}
-(CGPoint ) randomPoints;
#end
For some reason it complains on the randomPoints method saying Expected a Type? Why?
This could happen if CoreGraphics is not available. Then you'll need to link it explicitly:
Add proper import in your header to expose structs declared by CoreGraphics:
#import <CoreGraphics/CoreGraphics.h>

IOS duplicate interface definition

hello i have the code below on my .h file
import <UIKit/UIKit.h>
#interface NSFont : NSObject <NSCoding> {
}
#end
#interface NSParagraphStyle : NSObject <NSCoding> {
}
#end
and i get that error :
error: duplicate interface definition for class 'NSParagraphStyle'
i have no includes and no duplicates as pointed by some users
main.m imports
#import <UIKit/UIKit.h>
pref
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
NSParagraphStyle was added in the latest (unreleased) version of iOS. So you don't need to define it yourself. You probably need to change the name, don't use NS as prefix in that case.
NSParagraphStyle already exists (see here).
Any why are you using the NS namespace - if you want to creat your own classes, make your own namespace and precede your class names with that (i.e. MHParagraphStyle) so your paragraphStyle class won't clash with the built in one :)

Cannot find protocol declaration

I have two view controllers A and B, and they both have each other as their delegates.
When I did nothing except define the protocols at the beginning of the header files and #import the other's header file, I got two errors along the lines of -
cannot find protocol declaration for "BDelegate", which was showing in
A.h (where I wrote ) cannot find protocol declaration for
"ADelegate", which was showing in B.h (where I wrote )
Looking online, people had written earlier that the circular inclusion of header files could be leading to the problems. They recommended either using #include instead, or #class declaration like -
#class A
instead of
#import A.h
inside #import B.h
I have tried almost every combination of these imports, and #classes, and #include but still can't get rid of the warnings. Also, solutions online recommended moving the #import to the .m files but that didn't help either. Part of the reason is that the solutions online are kinda fuzzy - if you could break it down that would be great.
Any suggestions about what can be done to fix this?
-- BigViewController.h --
#import "BaseViewController.h"
#include "BaseViewController.h"
#class BigViewController;
#protocol BigViewControllerDelegate
-(void) BigViewController:(BigViewController *) bigView;
#end
#interface BigViewController : UIViewController <BaseViewControllerDelegate>
{
//delegate
id <BigViewControllerDelegate> delegate;
ivars...
}
#properties...
#end
--------------------------------------------------
-- BaseViewController.h --
#<UIKit/UIKit.h>
#import "BigViewController.h"
#include "BigViewController.h"
#class BigViewController;
#protocol BaseViewControllerDelegate
- (void) setParametersWithItemChosen:(Item *) item;
#end
#interface BaseViewController : UIViewController <...BigViewControllerDelegate...>
{
ivars...
//delegate
id <BaseViewControllerDelegate> delegate;
}
#properties...
#end
Let me reduce the sample even further, and label the lines:
VC1.h
#import "VC2.h" // A
#class VC1;
#protocol VC1Delegate // B
#end
#interface VC1 : UIViewController <VC2Delegate> // C
#end
VC2.h
#import "VC1.h" // D
#class VC2;
#protocol VC2Delegate // E
#end
#interface VC2 : UIViewController <VC1Delegate> // F
#end
Consider what happens when something #imports VC1.h: It reaches line A, then the import is processed. Line D does nothing because VC1.h was already imported. Then line E is processed. Then line F, and we get an error because we haven't gotten to line B yet so the protocol is not declared!
Consider then what happens when something #imports VC2.h: It reaches line D, then the import is processed. Line A does nothing because VC2.h was already imported. Then line B is processed. Then line C, and we get an error because we haven't gotten to line E yet so the protocol is not declared!
The first step is to reconsider whether both of these classes really need to be each other's delegates. If you can break the cycle, that would probably be the way to go. If not, you'll need to restructure your headers. The most straightforward way is probably to put the delegates into their own headers:
VC1Delegate.h
#class VC1;
#protocol VC1Delegate // B
#end
VC1.h
#import "VC1Delegate.h"
#import "VC2Delegate.h"
#interface VC1 : UIViewController <VC2Delegate> // C
#end
VC2Delegate.h
#class VC2;
#protocol VC2Delegate // E
#end
VC2.h
#import "VC1Delegate.h"
#import "VC2Delegate.h"
#interface VC2 : UIViewController <VC1Delegate> // F
#end
If you trace through the imports now, you'll see that the appropriate protocols will now always be declared before the #interface lines try to use them.
Write protocol declaration code above the #import lines
e.g.
#protocol -----
#end
import ----
#interface classname ---
I had almost the same problem, and I fixed it thanks to the answer above, but in a slightly different way.
all I did was to put the #import line after the protocol declaration in the header file.
hope I can help. and if anyone know that this is bad programing for some reason, pls let me know
I found another solution to this issue because I didn't really like the idea of just having some #imports in between the class and the protocol declaration.
Basically you just move <YourProtocolName> from the .h class file to the class extension in the .m file
So in your .m file you add
#interface YourClassName () <YourProtocolName>
#end
I don't know if this is really a good practise but it looks like a cleaner solution to avoid the import cycles.
Try putting the < BaseViewControllerDelegate > or < BigViewControllerDelegate > in implementation file rather then header file. It will work.
I followed the fix of moving the protocol before the import and it fixed the problem... the import included the delegate, so that was causing the problem.
But then I thought, why was I importing the delegate anyway? I wasn't referencing its properties and I wasn't calling any of its methods directly (that's what the protocol declare was for).
I tried commenting out the import of the delegate and saw where the error came up and found that what I was importing when I was importing the delegate was actually a declaration that the delegate was importing i.e. I was importing A (also my delegate), A was importing B, what I was actually using was B. So I left the import of A commented out and added an import for B. Then I could put the import-protocol order back the way it was.

How can I call a C function from Objective-C

I'm trying to figure out how to call a c function from an obj-c file. I've seen some good examples of how to do the opposite.
In the example below I have an objective c file. It contains a c function named setup.
I want to be able to create an instance of my obj-c file in the regular way and then call the setup function.
Header
#import <Foundation/Foundation.h>
void setup(int,float);
#interface Test : NSObject {
}
#end
Source
#import "Test.h"
#include <stdio.h>
#include <stdlib.h>
void setup(int val1,float val2)
{
//do something with values
}
#implementation Test
#end
View did load
Test *test =[Test alloc]init]
//this does not work
test.setup(6,1.4);
Just call setup(). As declared, is in no way tied to an object - it's just a regular C function.