How to run method with parameters in objective-c? [closed] - objective-c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I create a method in objective-c where I can run the method with different parameters each time. For example I want to be able to do something like this:
int thisMethod (int thisInt; NSString *thisString) {
int anotherInt = thisInt+2;
self.thisLabel.stringValue = thisString;
return 0;
}
So in this code, I want to run thisMethod with two parameters that can be used in the method.
i.e:
thisMethod(10; #"String");
Do I need to use a structure like this:
- (int) thisMethod:(id)sender{
//code here
}
If so, how do I use the parameters?

- (int)thisMethodWithInt:(int)thisInt andString:(NSString *)thisString {
int anotherInt = thisInt+2;
self.thisLabel.stringValue = thisString;
return 0;
}
Calling the Method then would look like this:
[self thisMethodWithInt:3 andString:#"My Super String"];
What you are describing is a c function and not a objective-c one..

You would have to pass the label object too, because self has no meaning in a C function. In Objective-C it's a hidden parameter that is passed to every method, but in a C function you have to pass it yourself (and use commas, instead of semicolons):
int thisMethod (int thisInt, NSString *thisString, id object) {
int anotherInt = thisInt+2;
object.thisLabel.stringValue = thisString;
return 0;
}
You probably have to explicitly set the type though, instead of using id, or the compiler will complain.
Again, use commas to separate arguments:
thisMethod(10, #"String", self);

Related

Access unicodeScalars from Objective-C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Reading this answer about detecting emojis in Swift 5 using unicodeScalars I'm wondering how I can access this in a Objective-C project. I know that Swift-Libs can be accessed in Objective-C, but is there an easier way to access just unicodeScalars and doing something similar to the linked answer?
following the answer it can be changed a little bit to make it work with Bridging back to ObjC..
#objc extension NSString {
var containsEmoji : Bool {
let uni = self as String
for scalar in uni.unicodeScalars {
switch scalar.value {
case 0x1F600...0x1F64F, // Emoticons
0x1F300...0x1F5FF, // Misc Symbols and Pictographs
0x1F680...0x1F6FF, // Transport and Map
0x2600...0x26FF, // Misc symbols
0x2700...0x27BF, // Dingbats
0xFE00...0xFE0F, // Variation Selectors
0x1F900...0x1F9FF, // Supplemental Symbols and Pictographs
0x1F1E6...0x1F1FF: // Flags
return true
default:
continue
}
}
return false
}
}
let's test..
NSString *normal = #"hello world";
NSString *special = #"🏖 badewelt";
if ([special containsEmoji]) {
NSLog(#"emoji inside %#",special);
} else {
NSLog(#"no emoji inside %#",special);
}
// or with dot syntax
if (normal.containsEmoji) {
NSLog(#"emoji inside %#",normal);
} else {
NSLog(#"no emoji inside %#",normal);
}
output
emoji inside 🏖 badewelt
no emoji inside hello world

Whenever I pass the value of a string list I'm getting an error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
fun sayHello(greet:String,itemsToGreet:List<String>){
itemsToGreet.forEach { itemsToGreet ->
println("$greet, $itemsToGreet")
}
}
fun main() {
val interestingThings = listOf("kotlin","program","comic")
sayHello(greet="hi", interestingThings)
}
A couple of problems:
You can't mix named and positional arguments in a method call. This results in a compilation error.
While not explicitly wrong, the fact that you're shadowing the itemsToGreet variable in your lambda expression is a code smell.
This fixes both:
fun sayHello(greet:String,itemsToGreet:List<String>) {
itemsToGreet.forEach { item -> // new variable name
println("$greet, $item")
}
}
fun main() {
val interestingThings = listOf("kotlin", "program", "comic")
sayHello("hi", interestingThings) // positional arguments
sayHello(greet="hi", itemsToGreet=interestingThings) // named arguments
}

IDE shows condition always false [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have this code.
enum EnumA {
VALUE_X(EnumA.EnumB.VALUE_J),
VALUE_Y(EnumA.EnumB.VALUE_J);
EnumB propertyC;
enum EnumB {
VALUE_J;
int propertyX;
}
EnumA(EnumB c) {
this.propertyC = c;
}
}
public class {
main(...) {
EnumA.VALUE_X.propertyC.propertyX = 1;
EnumA.VALUE_Y.propertyC.propertyX = 2;
if(EnumA.VALUE_X.propertyC.propertyX == EnumA.VALUE_Y.propertyC.propertyX) {
(Any statement)
}
}
}
So the problem is that my IDE shows that the condition is always false. But when I run the project the statement get executed (condition is true) why?
Since enum values are static VALUE_X and VALUE_Y are using the same instance of VALUE_J and have in turn the same value for propertyX.
You are setting it to 1 and right afterwards to 2.
You can check this by logging/printing out the information of both before, between and after the assignments. It should be 0, 1 and 2 respectively.
The reason for your IDE to state it's always false most likely is, that a simple check of the variables shows, they are different.

What is the best way to fill a slice from an iterator in Rust? [duplicate]

This question already has answers here:
How do I collect into an array?
(10 answers)
Closed 5 years ago.
I'm implementing FromIterator for [MyStruct;4] where MyStruct is a small Copy struct. My current implementation is
fn from_iter<I: IntoIterator<Item=MyStruct>>(iter: I) -> Self {
let mut retval = [Default::default();4];
for (ret, src) in retval.iter_mut().zip(iter) {
*ret = src;
}
retval
}
This works just fine, however I'm not sure that the for loop is as idiomatic as it could be. Is there perhaps a method like Slice::fill(iter) that could accomplish this more cleanly (and perhaps more efficiently)?
Loops are OK and they generally optimize very well.
Another solution may be to collect() into an ArrayVec. It avoids having to fill the array with a default value first.

How use block as parameter [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Obj-C: __block variables
In block programin How use block as parameter
NSArray *(^blockreturnarray) (NSArray *);
blockreturnarray= ^(NSArray * a)
{
NSLog(#"%#",a);
return a;
};
blockreturnarray(array1);
IN simple my Question I have one block create and also another block create So how In one block pass the parameter Block.
This can get hairy so I would use typedef's and it may look something like this
typedef void (^basicBlock)(void);
typedef void (^blockAcceptingBlock)(basicBlock);
Then
basicBlock block = ^ {
NSLog(#"Called from block passed as param");
};
blockAcceptingBlock parentBlock = ^(basicBlock childBlock) {
childBlock();
};
parentBlock(block);
Without the typedef's it gets a bit noisy in the definitions with all the round braces (keep in mind that these are simple blocks so the defs shown are still fairly readable)
void (^block)(void) = ^ {
NSLog(#"Called from block passed as param");
};
void (^parentBlock)(void (^childBlock)(void)) = ^(void (^childBlock)(void)) {
childBlock();
};
parentBlock(block);
Both output
#=> 2012-04-27 11:39:12.798 Untitled[19725:707] Called from block passed as param