calling objective c method from c function [duplicate] - objective-c

This question already has answers here:
How to call an Objective-C Method from a C Method?
(5 answers)
Closed 9 years ago.
I tried doing this in my objective C method
my_function(self);
my_function(void *param)
{
id self = param;
[self.Output insertText:#"Hello world"];
}
Output is of type NSTextView.
I get the following errors in compilation
Use of undeclared identifier 'id' in the line "id self = param".
I tried #import <Cocoa/Cocoa.h>, but didn't help. In the line [self.Output insertText:#"Hello world"];, I get "Expected expression" error.
Basically, what i am trying to do is to share some data periodically between my c function(where it's generated) and objective c method(where it's processed and displayed). I am total newbie in objective c, any help is greatly appreciated. If there is a better way to do this than calling objective c methods from C function, I am open to that.

You can not use objektive-c in .c files. But you can use c in .m files so if you need to mix make sure its a .m file.

Related

Getting class name in SWIFT equivalent to Objective C [self class]? [duplicate]

This question already has answers here:
Get class name of object as string in Swift
(32 answers)
Closed 3 years ago.
I am using the following syntax to debug my Objective C code, getting the class name. What is the equivalent in SWIFT?
NSlog (#"Classe = %#",[self class]);
You can use print("Class = \(type(of: self))" in Swift.

Objective-C to Swift 3 Method Name Change Issue [duplicate]

This question already has an answer here:
Use objective-c method in swift seems crossing keyword of swift language
(1 answer)
Closed 6 years ago.
I am trying to use a method from Objective-C in Swift 3, but Swift 3 is translating the signature to something invalid for the compiler.
- (void)doWhenReady:(void(^)(void))block onDone:(ErrorCallback)callback;
Gets translated to
do(whenReady: ()->(), onDone: ErrorCallback)
Where do becomes the keyword do and invalid syntax for the method. What solutions do I have to fix this without refactoring the Objective-C code?
In the Swift expression do(whenReady..., write do with backtick characters around it.

How to call an Objective C method in Swift that requires a runtime class as a parameter

I started programming in swift for about 2 weeks now and I still have some Objective C classes I would like to use in my app. One example would be calling this method from Swift:
+ (void)transitionFromThisVCClass:(Class<SomeProtocol>)aFromVCClass
ToThisClassVCClass:(Class<SomeProtocol>)aToVCClass
WithNavigationController:(UINavigationController *)aNav
WithDuration:(NSTimeInterval)aDuration
My problem is that I can't find a way to pass in a class type like I would have done in Objective C [SomeClass class]. Any help with this would be much appreciated. Thanks
UPDATE
If I'm trying to use MyClass.self like so:
ASFSharedViewTransition.addTransitionWithFromViewControllerClass(
RecommendationListViewController.self,
toViewControllerClass: RecommendationViewController.self,
withNavigationController: self.window?.rootViewController,
withDuration: 0.3)
ASFSharedViewTransition is an objective c class that I'm trying to call from swift
I get this error:
Cannot convert the expression's type '(RecommendationListViewController.Type,
toViewControllerClass: RecommendationViewController.Type,
withNavigationController: $T5??,
withDuration: FloatLiteralConvertible)'
to type 'FloatLiteralConvertible'
You can use foo.self, where foo is the class name. For example, MyClass.self

What's with the # sign in Objective C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the # symbol represent in objective-c?
I'm an ANSI C expert, taking my first steps to learn Objective C.
How should I think of/read the "#" that crops up everywhere?
At first, I thought of it as a compiler directive - "this in an instruction to the compiler to generate getter/setter" for "#synthesize someProperty".
But the "#" before strings doesn't seem to fit that model, and seems basically redundant.
Also would be glad of any advice for C experts on "how to quickly learn Objective C". Thanks,
Peter
# is just a character unless you use it with full context
#interface //For declaring a class
#property // For creating a property
#synthesize // For synthesising property
#implementation //For implementing a class
#end //For ending #interface or #implementation
#"Some String" //Represents a string
#try
#catch
and so on.
So simple # is nothing but when combined it becomes the directive

Beginner Objective C syntax observation [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does #synthesize window=_window do?
How come when you #synthesize something like buttonPressed , You need to do this:
#synthesize buttonPressed = buttonPressed_;
I've been following some tutorials and this keeps coming up. Why?
You do not have to do it that way.
By default, #synthesize variableName does work, if your synthesized accessors shall have the same name as your instance variable.
In your example, the instance variable is called buttonPressed_ but your accessor methods will omit the _ and thus just be called setButtonPressed and buttonPressed.