Concatenate variable IBOutlet inside a loop? [closed] - objective-c

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.

Related

Check if object created: good style? [closed]

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

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

What happens when you try to cast an object that is null in objective c? [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've got the following code:
[application.tripHandler createTrip:picker.date
pickUpAddress:[ModelUtil addresFragmentFromGeoAddress:pickUpAddress]
orPickUpLocation:pickUpFavorite
andDropOffAddress:[ModelUtil addresFragmentFromGeoAddress:dropOffAddress]
orDropOffLocation:dropOffFavorite forDriver:application.currentDriver.driverId
completion:^(NSObject *entity)
{
if(application.dispatchVehicle)
{
Trip *createdTrip = (Trip *)entity;
What happens when entity is null?
If entity is nil, then createdTrip will be nil as well.
(nil and NULL are technically the same thing, but nil is semantically the null object value whereas NULL is the null pointer).
Casting a pointer does not actually do anything at runtime. It merely modifies the type being pointed-to, which then affects subsequent operations on the resulting value. But the actual pointer value will remain the same through the cast.
createdTrip will be nil object.
In obj-c, a cast doesnt do anything during runtime, so you will just assign nil.

Making A Math Equation [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
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.

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!