Check if object created: good style? [closed] - objective-c

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;
}

Related

NSRangeException randomly crash app in object at index [closed]

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.

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 to implement methods found in the Mac Developer Library [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 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

Objective C Method signatures problem [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi I have declared a method in one of my classes called HttpWorker. The method declaration is
-(void) setRequestParameters:(NSString *)parameters iRequestCode:(double)iRequestCode initialSleep:(long)initialSleep;
I am using trying to call this method from another class called NetManager. I wrote following code for this
NSString *paramStr = #"jc=2";
HttpWorker *httpWorker = [[HttpWorker alloc] init];
double requestCode = [[NSDate date] timeIntervalSince1970];
[httpWorker setRequestParameters:paramStr iReqeustCode:requestCode initialSleep:initialSleep];
But when I compile my code, xcode gives me following warning.
warning: 'HttpWorker' may not respond to '-setRequestParameters:iRequestCode:initialSleep:'
Can anyone please tell me where i am wrong?
Thanks and Best Regards
You have a typo:
iReqeustCode:
Should be:
iRequestCode:
Also you have not defined initialSleep. That should result in compile error!