Making A Math Equation [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
I am teaching myself Objective - C and I tried out some code to do a math equation. Can you please tell me what I am doing wrong?
- (IBAction)mathEquation:(id)sender {
int a = 6;
int b = 2;
self.showAnswer.text = int a + int b;
}
Can someone please rewrite to code the correct way and post it? Thank you!

int result = a + b;
self.showAnswer.text = [NSString stringWithFormat:#"%d", result];
In Objective-C, you can't just assign a number to a string, it won't work. As you see in my code, you need to convert from a numerical value into a string. The method [NSString stringWithFormat:] lets you do that, and what kind of number(s) you want to include in the string. E.g. the format expression %d is for an integer value.
Also, once you declared your variables (int a, etc.), you don't need to declare them again, you'll get an error.
I would strongly advice reading up on the basic syntax of Objective-C.

Related

Concatenate variable IBOutlet inside a loop? [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 8 years ago.
Improve this question
I have 13 textfield and I need to modify their values ​​according to the parameters received from an array inside a loop:
IBOutlet UITextField *c1_1;
....
IBOutlet UITextField *c1_13;
int xis;
int xis2;
for(xis=0;xis<14;xis++){
xis2++;
[NSString stringWithFormat:#"c1_%d.text",xis2] = lstaInfo[xis];
}
But the command is not working, I wish you could explain to me the possible solutions to this problem
Oh lordy. You have tons of problems.
You don't initialize xis2, but then you increment it. It may contain random garbage.
You try to assign something to a call to stringWithFormat. That is not valid Objective C. Should you flip the left and right sides of that assignment?
lstaInfo[xis] = [NSString stringWithFormat:#"c1_%d.text",xis2] ;
You also say "the command is not working" without either telling what you are trying to do, or how your code fails to accomplish that task.
You need to provide a much clearer explanation if you actually want help. (but fix the above problems first)
This isn't the right way to do this. You want an IBOutletCollection.

Working with NSArray in Obj C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am currently trying to make a simple craps app for the iPhone.
In the model I have a method:
-(NSString *)resultsOfRoll:(int)firstRoll :(int)secondRoll
{
NSString *results = #"";
NSArray *naturalNumbers = #[#7,#11];
NSArray *losingNumbers = #[#2, #3, #12];
NSArray *pointNumbers = #[#4,#5,#6,#8,#9,#10,#11];
int sum = firstRoll + secondRoll;
if(sum in naturalNumbers)
{
return #"You rolled a natural! You won";
}
return results
}
But I am getting an error. I am pretty new to Obj C and I haven't used much enumeration(If that is wrong please correct me) yet. Could someone let me know if I am initializing the loop correctly and how I can check to see if the sum (roll + roll) is in an array?
Also, does my method name look correct? I am coming from Java and these method sigs are still a little confusing for me.
if(sum in naturalNumbers) obviously isn't valid syntax. You're using something like 'for in' and with an int instead of an object.
What you want is:
if ([naturalNumbers containsObject:#(sum)]) {
Which asks the array if it contains an NSNumber instance containing the sum.
Your method doesn't name the second parameter, which is legal but not encouraged. It would be better as:
-(NSString *)resultsOfRoll:(int)firstRoll secondRoll:(int)secondRoll

How do I identify the return type of a method during runtime? [closed]

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
For the sake of this question, let us say that I have an Objective-C class consisting of the following methods:
- (float)method1;
- (CGPoint)method2;
- (NSString *)method3;
- (void)method4;
How can I identify the return types of all the methods above dynamically during runtime?
You can use Objective-C runtime functions to get this information, but there are limitations. The code below will do what you want:
Method method1 = class_getInstanceMethod([MyClass class], #selector(method1));
char * method1ReturnType = method_copyReturnType(method1);
NSLog(#"method1 returns: %s", method1ReturnType);
free(method4ReturnType);
Method method2 = class_getInstanceMethod([MyClass class], #selector(method2));
char * method2ReturnType = method_copyReturnType(method2);
NSLog(#"method2 returns: %s", method2ReturnType);
free(method4ReturnType);
Method method3 = class_getInstanceMethod([MyClass class], #selector(method3));
char * method3ReturnType = method_copyReturnType(method3);
NSLog(#"method3 returns: %s", method3ReturnType);
free(method4ReturnType);
Method method4 = class_getInstanceMethod([MyClass class], #selector(method4));
char * method4ReturnType = method_copyReturnType(method4);
NSLog(#"method4 returns: %s", method4ReturnType);
free(method4ReturnType);
Output:
>>method1 returns: f
>>method2 returns: {CGPoint=dd}
>>method3 returns: #
>>method4 returns: v
The string returned by method_copyReturnType() is an Objective-C type encoding string, documented here. Note that while you can tell if a method returns an object (encode string "#"), you can't tell what kind of object it is.
I'd be curious why you're interested in doing this. Especially for a new Objective-C programmer, my first inclination is to encourage you to think about whether this is actually a good design choice. For the methods you've asked about, this is pretty straightforward, but methods with more exotic return types can lead you into some trickier stuff with type encodings.

NSInteger is NSInteger error [closed]

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
-(IBAction)button:(id)sender{
If (cardsinplay >= 16){
NSNumber *cardValue = carsAndValue[14];
NSInteger *Value = [cardValue integerValue[;
From what i know this should read the NSNumber from the array and change it into a NSInteger
But i get this error.
Incompatible integer to pointer conversion initialization 'NSInteger*'(aka 'int*') with an expression of type 'NSInteger'(aka 'int*')
-[NSNumber integerValue] returns a NSInteger, not a pointer to one.
NSInteger *Value = [cardValue integerValue];
should be
NSInteger value = [cardValue integerValue];

NSString contains anything ".*" to show it [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 8 years ago.
Improve this question
Hi I have a problem with strings. I want to add :
NSString *termo = [NSString stringWithFormat:#"%#%#: %# ", #"~00000000:",nazwa, #".*"];
This .* is anything. How can I use it?
Your question is very unclear, however your comment "I have some string which I get from server. I want to parse this string with this" seems to suggest:
that you have a string obtained from somewhere;
this string should contain the text you have stored in the variable nazwa; and
you wish to find the text that follows whatever nazwa contains.
If this guess is correct then the following code fragment might help, it does not contain any checks you need to make to verify the input actually contains what you are looking for and is followed by something - check the documentation for the methods used to see what they return if they don't locate the text etc.:
// a string representing the input
NSString *theInput = #"The quick brown fox";
// nazwa - the text we are looking for
NSString *nazwa = #"quick";
// locate the text in the input
NSRange nazwaPosition = [theInput rangeOfString:nazwa];
// a range contains a location (offset) and a length, so
// adding these finds the offset of what follows
NSUInteger endofNazwa = nazwaPosition.location + nazwaPosition.length;
// extract what follows
NSString *afterNazwa = [theInput substringFromIndex:endofNazwa];
// display
NSLog(#"theInput '%#'\nnazwa '%#'\nafterNazwa '%#'", theInput, nazwa, afterNazwa);
This outputs:
theInput 'The quick brown fox'
nazwa 'quick'
afterNazwa ' brown fox'
HTH
.* is a regular expression that is used to match anything, but if you just want to see if an NSString isn't empty, you're better off doing something like this
![string isEqualToString:#""]