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
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
NSDictionary *latestCollection=[json objectWithString:responseString error:&error];
NSDictionary *data=[latestCollection valueForKey:#"results"];
if([data count]>0 && data!=nil)
{
arrayAddress=[[NSMutableArray alloc]initWithArray:[data valueForKey:#"formatted_address"]];
/*********Showing address of new location******/
[lblAddress setText:[NSString stringWithFormat:#"%#",[arrayAddress objectAtIndex:0]]];
[lblPickUpAddress setText:[NSString stringWithFormat:#"%#",[arrayAddress objectAtIndex:0]]];
}
else
{
NSLog(#"Address not available");
}
Your problem is your if clause:
if([data count]>0 && data!=nil)
if will evaluate from left to right, so first, it accesses data for the count, but it may be nil, so simply switch the expressions like so:
if( data!=nil && [data count]>0)
the if clause will stop, as soon as one of the expressions if false, so it will do a check for nil, if its NOT nil, then it will access data to get the count. If data is nil, then the data will not be accessed. Thats why your app crashes.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I recently did some functions like the following one:
-(BOOL)registerSomethingWithParameter:(Parameter*) param
{
Something* some = nil;
if ([self checkParameter:param])
{
some = [[Something alloc] myInitCallWithParameter:param];
}
return (some ? YES : NO);
}
There are many discussions about using the ? in code. What do you think? Is this a proper way to tell the calling function, that everything worked well without returning an object?
I also thought about: isn't it better to check for valid parameter in myInitCallWithParameter: within the Something-Definition, but mostly these Classes are very small and store only a few values. So everything that could result in creating a nil is checked when entering the if.
I don't think there's any problem in using ? instead of if/else. I see a lot of programmers using it and I use it myself. Your code style is fine.
Why not just do:
-(BOOL)registerSomethingWithParameter:(Parameter*) param
{
return [self checkParameter:param];
}
I'm assuming checkParamter returns a bool. In which case you don't even need this registerSomethingWithParameter function as it just creates a local variable that isn't used anywhere anyway ? Unless you've just written this as an example. :-)
While your approach is perfectly viable, a better option might be to simply return the created object itself; like so:
-(id)registerSomethingWithParameter:(Parameter*) param
{
Something* some = nil;
if ([self checkParameter:param]) {
some = [[Something alloc] myInitCallWithParameter:param];
[self registerSomething:some];
}
return some;
}
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.
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.
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 9 years ago.
Improve this question
I'm a totally new to Objective C programming and just need to check how would I implement something I find on the Mac Developer Library.
For example I found this code in the Mac Developer Library:
- (BOOL)containsObject:(id)anObject
Are the steps I need to implement this method?
in the .h file add
-(BOOL)containsObject:(id)anObject;
then in the .m file implement it like this;
- (BOOL)containsObject:(id)anObject
{
//What do I place here to search my array with the objects?
return YES;
}
Can someone help me with an example how I would search an array of numbers and use this method?
This is my array:
NSArray *first;
first = [NSArray arrayWithObjects:1,2,3,4,5, nil];
Do you mean "implement" - as in write your own version - or "use"? You seem to start with the former and end with the latter...
How do you use it? Well taking your example, corrected:
NSArray *first = [NSArray arrayWithObjects:#1, #2, #3, #4,# 5, nil];
The #1 etc. is a shorthand to create an NSNumber object - you can only put object references and not primitive values into an NSArray. The shorthand effectively expands to [NSNumber numberWithInt:1] etc.
Now to use containsObject::
BOOL containsTheAnswerToEverything = [first containsObject:#42];
This will set the BOOL variable to YES if the array contains an NSNumber representing 42, and NO otherwise.
How do you implement it? You examine each element of your array in a loop comparing each to the object you are looking for, same algorithm you would use in C (which it seems you know).
If you are asking how to define a class, then you should read up on Objective-C, but your point 2 is the correct outline.
HTH
In respect to the first question, you can do just that. An example to give you a better visual understanding would be this:
YourClassName.h
#import <Foundation/Foundation.h>
#interface YourClassName : Subclass //replace subclass with something like NSObject or whatever you need
{
}
- (BOOL)containsObject:(id)anObject;
#end
and...
YourClassName.m
#import "YourClassName.h"
#implementation YourClassName
- (BOOL)containsObject:(id)anObject
{
//Insert function body here
}
#end
As for your second question, I am not really very familiar with using NSArray or loading it using that strange function. My advice would be using (NSArray)anObject instead of (id)anObject as you can load the array directly into your function and create your search parameters there. I'm not sure exactly what object you are looking for in terms of containsObject. Are you seeing if it contains the number? If the value contains the array? specify it a bit and I may be able to dig up a better answer for you
EDIT:
It occurred to me that you are probably just looking for the number inside of the array seeing as you are newer to Objective-C. To accomplish what you want you have a couple options. Both require the change of your function. The first would be just changing the function to this:
- (BOOL)containsObject:(int)object fromArray:(int*)array length:(int)length;
In YourClassName.h. Now you can always change your parameters to different data types but this will work for your integers. You can also do this without the length parameter but I figure it saves us some code (personal preference). And in the .m file:
- (BOOL)containsObject;(int)object fromArray:(int*)array length:(int)length
{
for(int i = 0; i <= length; i++)
{
if (array[i] == object)
{
return YES;
}
}
return NO;
}
second would be just without the length option and a bit more code