I am a calling c++ method from objective c:
C++ Method:
void TestMethod( size_t& outputSize,
OutputArray& outputArray );
Objective C:
-(void) testMethodObjc : outputSize,
OutputArrayObjc : outputArray
{
TestMethod( outputSize, [outputArray getArray ]);
}
How do I accomplish this? I hear from other postings that objective-c does not support pass by reference.
You should be able to - Obj-C is a strict subset of C. Just make sure that the file the code is in is a .mm file - not just .m
Objective-C, like C, does not support pass by reference.
Like C, you can take the address of the variable and pass a pointer instead. And to manipulated the original variable in the function you would need to dereference the pointer.
Related
I have created a CLogger objective C class and defined below macro
#define CLogError(fmt, ...) //remaining code
I am able to access CLogError in objective C files and call it. But above macro is not accessible in swift file. How can I call this from swift?
You can't, at the moment. You can always define it in Swift as a global function:
func CLogError(fmt : String, strings : String...) { ... }
I need use a Objective-C SDK in my Swift project, the Objc demo is in below
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(#"reslut = %#",resultDic);
}];
It pass a block to payOrder:orderString:: function, but when I call it in Swift, the auto complete help me generate these code
AlipaySDK.defaultService().payOrder(orderString, fromScheme: self.aliAppScheme, callback: { ([NSObject : AnyObject]!) -> Void in
println("Pay Success")
})
in Swift the closure input parameter has no name, in Objc it named resultDict, but in Swift I don't know how to get it pointer, Please help me, Thanks
In the objective C block, it takes an NSDictionary parameter. With Swift closures, the closure is already typed so you don't have to declare NSDictionary as the type and you really don't even need -> Void. Also the , callback: is extraneous in Swift as well because of trailing closures so your final product should be:
AlipaySDK.defaultService().payOrder(orderString, fromScheme: self.aliAppScheme) { resultDict in
println("Pay Success")
}
I found an example for IOKit:
var notification:io_object_t
let matching:NSDictionary = IOServiceNameMatching("IODisplayWrangler").takeRetainedValue()
let displayWrangler = IOServiceGetMatchingService(kIOMasterPortDefault, matching)
let notificationPort = IONotificationPortCreate(kIOMasterPortDefault)
IOServiceAddInterestNotification(notificationPort, displayWrangler, kIOGeneralInterest, displayPowerNotificationsCallback, nil, ¬ification)
CFRunLoopAddSource (CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode);
IOObjectRelease (displayWrangler);
The above example is clear to me - so far. But IOServiceAddInteresNotification wants a callback function. In it's simple to do this, by implementing the C-Style function somewhere in the .m-file.
The documentation says that I have to use a callback of type IOServiceInterestCallback.
In C-Style it is defined as follows:
typedef void ( *IOServiceInterestCallback)( void *refcon, io_service_t service, uint32_t messageType, void *messageArgument );
And on objC everything seems to work out perfectly.
What is the equivalent solution in swift? How do I declare the callback function without creating a C or objC file for this?
Any ideas?
Cheers,
Jack
You cannot create C function like callbacks in Swift as closures are not compatible with CFunctionPointer. You can implement some workaround in Objective-C or C. Example is describe in Objective-C Wrapper for CFunctionPointer to a Swift Closure
I went through quite a number of websites, but everywhere they have given how to write a wrapper for a c++ library.
Now, I have a .c file which i want to integrate to my application. Since Objective c is an objected oriented extension of c, will i actually have to write wrapper for it? if yes, how to do it? if i dont have to, then how to use the c code in my project?
EDIT : ok.. what i have to do is add the file into my project and use the functions? how exactly to do it?? just like normal c call? what if i have to pass parameters?
Let say i have a function which returns a string, first of all how do i call that function? and if it returns a string, can i store that value in a normal NSString?? or should i declare a c string for it??
Thanks in advance
You can simply use your C code as usual:
-(void)writeString:(NSString*)data toFile:(NSString*)filename {
FILE* output = fopen([filename UTF8String], "w");
fprintf(output, "%s", [data UTF8String]);
fclose(output);
}
For other .c files, simply #include the corresponding header. Then, you can just call its functions.
For example, if this was foo.h:
int add(int a, int b);
And foo.c:
#include "foo.h"
int add(int a, int b) {
return a + b;
}
Then in your Objective-C code (Bar.m):
-(int)addA:(int)a andB:(int)b {
return add(a, b);
}
That is basically a wrapper right there. However, wrappers are not needed for C code in Objective-C. Even C++ functions do not need wrappers, as there are .mm files which are Objective-C++ sources.
Edit:
To call C functions with parameters, just call them with parameters. Literally ANY valid C program is also a valid Objective-C program. If it can compile as a .c file, it'll compile as a .m file.
To convert a C string (char*) to an NSString:
const char* myString = "Hello!";
NSString* myNSString = [NSString stringWithUTF8String:myString];
Is there any dramatic difference between functions and methods in Objective -C?
First, I'm a beginner in Objective-C, but I can say what I know.
Functions are code blocks that are unrelated to an object / class, just inherited from c, and you call them in the way:
// declaration
int fooFunction() {
return 0;
}
// call
int a;
a = fooFunction();
While methods are attached to class / instance (object) and you have to tell the class / object to perform them:
// declaration
- (int)fooMethod {
return 0;
}
// call
int a;
a = [someObjectOfThisClass fooMethod];
It is even simpler; a method is just a C function with the first two argument being the target of the method call and the selector being called, respectively.
I.e. every single method call site can be re-written as an equivalent C function call with absolutely no difference in behavior.
In depth answer here: Why [object doSomething] and not [*object doSomething]? Start with the paragraph that says "Getting back to the C preprocessor roots of the language, you can translate every method call to an equivalent line of C".