Who calls the function in objective-c? [duplicate] - ios

This question already has answers here:
Objective-C find caller of method
(12 answers)
Closed 7 years ago.
Is it possible to know which function calls the current function in the objective-c?
for example, I have 100 functions give a call to function X. In the function X, are we able to define which function from 100 functions has already called?
Thanks.

I'm using a define directive :
#define CALLER_OF_METHOD NSLog(#"My Caller: [%#]", [[[[NSThread callStackSymbols] objectAtIndex:1] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"[]"]] objectAtIndex:1])
please try, let me know if it's works for you chipbk10

if you dont want to write any code, put a break point in the function, then on the left it will show you the calling tree and you can see what functions were called in order to get to that breakpoint

Need to run application set break point in function X. When application stops at break points check functions in Debug navigator in xcode. you will know that which function called function X.See the sequence in below image.

Related

Swift assign a float variable [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I found a issue that seems strange, or something I don't understand. The following is a very simple variable assignment:
let f: Float = 310.15
In debugger, I see it is 310.149994
I need to send the value as 310.15 to server, but 310.149994 is received instead.
Is this a Swift compiler or runtime bug? Or any way I can ensure 310.15 float is sent? (I cannot change the server API signature)
Try this one
let f : Float = 310.149954
print(round(1000*f)/1000)
result : 310.15

Swift compiler error: missing argument labels [duplicate]

This question already has answers here:
Swift 3 first parameter names
(5 answers)
Closed 5 years ago.
I run Swift on Xcode 8.3.3. Here I get an error regarding closures.
It seems that this part is all correct [var intro = introToFriends("Jim", "Pam")
intro] yet isn't clicking.
What's the issue?
Later Swift versions require explicit argument labels when calling a function. In your case it needs to read:
var intro = introToFriends(friendOne: "Jim", friendTwo: "Pam")
Alternatively, you can allow for omitting the use of argument labels if you add underscores to the parameter labels in the function declaration such as:
func introToFriends(_ friendOne: String, _ friendTwoString) {
...
}
For more details check the official Apple documentation on function argument labels.

How to make sure my array structure is the same in both Objective-C and Swift?

Am calling a .mm (objective-c / c++) class method from my swift viewController via linked headers.They are successfully linked. However, I am struggling to pass correct data types that match as arguments.
Here is where I call the function in swift
OpenCVWrapper.thefunc(array1, otherstuff);
...array1 is of type [[Int]]
and here is the definition in objective-c
+(NSString*) thefunc: (int[][2])array1 otherstuff(int)other{
but i get the error
Cannot convert value of type '[[Int]]' to expected argument type 'UnsafeMutablepointer<(Int32)(Int32)>!'
As discussed in another similar question i posted, I need to make sure swift array1 is in fact of type [(Int32, Int32)]. Now, no errors get thrown.
BUT, when I dry run the Objective-C method, the array1 only shows with 2 values (after break point). Where as in Swift i pass through 9 (of 2), which is what I want.
My question is, how can I confirm my array in objective-c will be the same structure to the one in swift?
It appears that you are relying on the debugger to show you the values passed in as array1. To verify programmatically that correct values are passed, you can do the following in thefunc():
+(NSString*) thefunc : (int[][2])array1 {
for (int i = 0; i < 9; i++)
printf("Tuple %d is: %d, %d\n", i, array1[i][0], array1[i][1]);
...
}
9 is the number of 2-int tuples you passed. This count should probably be passed as one of the things in otherstuff, unless it is always the same.
BTW, you should probably pass array1 as in-out in Swift (note the &):
OpenCVWrapper.thefunc(&array1,...)

How to fix Xcode 7.3 warning: `init` is deprecated: it will be removed in Swift 3: Use `enumerate()` method on the sequence [duplicate]

This question already has answers here:
Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence
(3 answers)
Closed 6 years ago.
From recent update of Xcode 7.3, I started seeing this message. I am using a sequence for in loop as below:
for (index, product) in EnumerateSequence(self.products) {
//Do something with the product
//Do something with the index
}
The note is on the EnumerateSequence.
If you're wondering why they added this warning and are going to remove EnumerateSequence.init, it's because EnumerateSequence is an implementation detail of the enumerate method. They want you to use enumerate and not rely on how it's implemented.
After some testing, this is the solution to use from Swift 2.2 onwards in case you want to use both the index and the object:
for (index, product) in self.products.enumerate() {
//Do something with the product
//Do something with the index
}
Remove EnumerateSequence and use your Array.enumerate() method

Execute ObjC function from NSstring [duplicate]

This question already has answers here:
Using NSString to call a function in XCode 4.x
(5 answers)
Closed 8 years ago.
this is stupid question for execute some function in objC, for example like this:
NSString *function = #"-(void)execute{NSLog(#"123");}"
how to execute that string become a function?
This is not possible, since Objective-C is compiled, not interpreted.
What you can do is add a -(void)execute {...} method in your code. Then if for some reason you need to call this method using a string, you can use [self performSelector:NSSelectorFromString(#"execute")].
But the string can not include any code. It must be limited to being an existing method's name.
I think you are looking for,
1. No Parameter
SEL aSelector = NSSelectorFromString(#"execute");
[self performSelector:aSelector];
Simulation:
-(void)execute;
2. Single Parameter
SEL singleParamSelector = NSSelectorFromString(#"methodWithOneParam:");
[self performSelector:singleParamSelector
withObject:first];
Simulation:
-(void)methodWithOneParam:(NSString*)first;
3. Multi Parameter
SEL doubleParamSelector = NSSelectorFromString(#"methodWithFirst:andSecond:");
[self performSelector: doubleParamSelector
withObject: first
withObject: second];
Simulation:
-(void)methodWithFirst:(NSString*)first andSecond:(NSString*)second;
Can you please describe in detail, what you really want to do. What you describe pretty much looks like runtime code injection, if so just check the following docs about class_addMethod:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/c/func/class_addMethod
and check this good description:
http://distriqt.com/post/846
Not possible. As the others have pointed out, you can create a selector from a string using NSSelectorFromString, and then you can use performSelector:withObject: or it's variants to invoke that method.

Resources