String Help - Objective C - objective-c

I'm trying to make an app that will respond to your command when inserted. So you type in any text in the first box and press enter. It will respond with a response in the 2nd field. I'm not sure how the coding is done here. I'm having trouble with the "if inputbox text = #"whatever", I'm pretty sure that is completely off. Here is the code I have so far (not iphone sdk):
#import "HeliosControl.h"
#implementation HeliosControl
- (IBAction)quitButton:(NSButton *)sender {
}
- (IBAction)sendButton:(NSButton *)sender {
if (inputBox *********) // <------ What do I put in for the asterisks?
{
[outputBox setStringValue:#"Welcome to the SYSTEM"];
}
else
{
[outputBox setStringValue:#"I do not understand your command."];
}
}
#end
BTW I'm a complete noob since I started Objective-C like a week ago.
Second Question:
This is a very simple one, but what would be the coding for closing an application? This is for my quit button.

You want if ([[inputBox stringValue] isEqualToString:#"whatever"]) (assuming inputBox is an NSTextField — otherwise, use the appropriate method for that class to get a string out of it).
Oh, and you can quit the application with [NSApp terminate:self].

Chuck's answer is spot on, but I thought it worth expanding on why you've had problems. There are a number of mistakes in your line:
"if inputbox text = #"whatever"
a) In Objective C you have to use == to check if x is equal to y. So the if statement would be:
if (myFirstVariable == mySecondVariable) { // Do something }
b) A string variable is actually a more complicated thing than a variable just holding a number. That variable's value will actually be the memory address where it is stored. Also, you will usually actually only be using a pointer (denoted by the * when you declare a variable) to the variable.
This means that if you type the following:
if (myFirstVariable == #"Some text")
or
if (myFirstStringVariable == mySecondStringVariable)
Then you're actually only checking for whether they both point to the same bit of memory! Not whether the text is the same. This is why as Chuck explained you need to use the [isEqualToString] method.
Hope that helps!

Related

Xcode save value of variable “before and after”

I currently have this code attempting to save the value of a specific variable in before, and then re-saving the new value of the variable a second later to a variable named after.
- (void)getAfter {
after = Currentitems;
NSLog(#"%d, %d", before, after);
}
- (void)detectItems {
before = Currentitems;
[self performSelector:#selector(getAfter) withObject:nil afterDelay:1];
}
The function "detectBPS" runs every second as well. However, both of the before and after variables equal the same thing. How can I achieve what I am attempting to do?
Edit: I've read the suggestions. However, how can I implement KVO? Nevertheless, I'd still rather use (and am more open to) different/simpler suggestions as well.

objective-c: variable is not a CFString, how to correctly assign string value to an object's property?

- (IBAction)start:(UIButton *)sender {
NSString *t=#"123"; // line 1
self.detailDescriptionLabel.text=t;
}
The syntax check is correct, but when I build and run it on iphone, it breaks out at line 1 and sending the error message like "variable is not a CFString"
How to solve this problem?
And it is working when I wrote this:
- (IBAction)start:(UIButton *)sender {
self.detailDescriptionLabel.text=nil;
}
I really think the problem lies in how to correctly assign a string constant to a UILabel's text property.
Ok, it is stupid... I wrongly pressed the shotcut key and a breakpoint to it.....So actually there is no mistakes in the code, just my mistake in the XCode IDE.

Prevent NSTextField from being left blank

I have a NSTextField with an NSNumberFormatter inside of it. I've seen textfields that if you leave them blank it just puts whatever number was in it previously back into it. I'm curious if there's a setting in Interface Builder that provides this behavior. I can't seem to find it, but I'm fairly new to IB and might not be looking in the right spot.
Thanks
There's no behaviour that I know of in IB other than the default value (which won't help here), but you could use NSTextFieldDelegate (extension of NSControlTextEditingDelegate) to monitor when editing finishes, using control:textShouldEndEditing: you can throw a value back into the box if it's left blank. You can read about NSTextFieldDelegate here.
If you want to leave just back some default value for case the user deleted the input
1) Subclass NSNumberFormatter
2) Implement (will put a 0, if empty)
- (NSString *)stringForObjectValue:(id)obj {
if (obj == nil) {
return #"0";
}
return [super stringForObjectValue:obj];
}
3) set the class in IB

Use an If statement with the button name in Objective-C (Cocoa/iPhone SDK)

I have to implement a small feature in an iPhone app and was wondering if there was a way to do use an if statement where the condition is the string of a button.
Here’s a sample of the code in question:
- (IBAction)someMethod:(id)sender{
UIButton *button = (UIButton *)sender;
if ( button.titleLabel.text == “SomeText” )
{
//do something
}
else
{
// some other thing
}
Now I can’t make it work, because I think I’m using the wrong code in button.titleLabel.text. I’ve even tried #“SomeText”), but I always end up in //some other thing.
What am I doing wrong?
Thanks.
What you're currently doing is comparing two pointers to objects, the objects button.titleLabel.text and #"SomeText". As both point to different places in the memory, the comparison will return NO.
If you want to compare the values of both NSString objects, however, you can use [button.titleLabel.text isEqualToString:#"SomeText"].
Also note that "SomeText" is not the same as #"SomeText"! The first is a regular C string, where the last one is a Cocoa NSString object.

Passing arguments by value or by reference in objective C

I'm kind of new with objective c and I'm trying to pass an argument by reference but is behaving like it were a value. Do you know why this doesn't work?
This is the function:
- (void) checkRedColorText:(UILabel *)labelToChange {
NSComparisonResult startLaterThanEnd = [startDate compare:endDate];
if (startLaterThanEnd == NSOrderedDescending){
labelToChange.textColor = [UIColor redColor];
}
else{
labelToChange.textColor = [UIColor blackColor];
}
}
And this is the call:
UILabel *startHourLabel; // This is properly initialized in other part of the code
[self checkRedColorText:startHourLabel];
Thanks for your help
Objective-C only support passing parameters by value. The problem here has probably been fixed already (Since this question is more than a year old) but I need to clarify some things regarding arguments and Objective-C.
Objective-C is a strict superset of C which means that everything C does, Obj-C does it too.
By having a quick look at Wikipedia, you can see that Function parameters are always passed by value
Objective-C is no different. What's happening here is that whenever we are passing an object to a function (In this case a UILabel *), we pass the value contained at the pointer's address.
Whatever you do, it will always be the value of what you are passing. If you want to pass the value of the reference you would have to pass it a **object (Like often seen when passing NSError).
This is the same thing with scalars, they are passed by value, hence you can modify the value of the variable you received in your method and that won't change the value of the original variable that you passed to the function.
Here's an example to ease the understanding:
- (void)parentFunction {
int i = 0;
[self modifyValueOfPassedArgument:i];
//i == 0 still!
}
- (void)modifyValueOfPassedArgument:(NSInteger)j {
//j == 0! but j is a copied variable. It is _NOT_ i
j = 23;
//j now == 23, but this hasn't changed the value of i.
}
If you wanted to be able to modify i, you would have to pass the value of the reference by doing the following:
- (void)parentFunction {
int i = 0; //Stack allocated. Kept it that way for sake of simplicity
[self modifyValueOfPassedReference:&i];
//i == 23!
}
- (void)modifyValueOfPassedReference:(NSInteger *)j {
//j == 0, and this points to i! We can modify i from here.
*j = 23;
//j now == 23, and i also == 23!
}
Objective-C, like Java, only has pass-by-value. Like Java, objects are always accessed through pointers. "objects" are never values directly, hence you never assign or pass an object. You are passing an object pointer by value. But that does not seem to be the issue -- you are trying to modify the object pointed to by the pointer, which is perfectly allowed and has nothing to do with pass-by-value vs. pass-by-reference. I don't see any problem with your code.
In objective-c, there is no way to pass objects by value (unless you explicitly copy it, but that's another story). Poke around your code -- are you sure checkRedColorText: is called? What about [startDate compare:endDate], does it ever not equal NSOrderedDescending? Is labelToChange nil?
Did you edit out code between this line
UILabel *startHourLabel;
and this line?
[self checkRedColorText:startHourLabel];
If not, the problem is that you're re-declaring your startHourLabel variable, so you're losing any sort of initialization that was there previously. You should be getting a compiler error here.
Here are the possibilities for why this doesn't work:
the label you pass in to checkRedColorText is not the one you think it is.
the comparison result is always coming out the same way.
... actually, there is no 3.
You claim you initialised startHourLabel elsewhere, but, if it is a label from a nib file, you should not be initialising it at all. It should be declared as an IBOutlet and connected to the label in the nib with interface builder.
If it is not a label in the nib i.e. you are deliberately creating it programmatically, you need to check the address of the label you initialise and check the address of the label passed in to checkRedColorText. Either NSLog its address at initialisation and in checkRedColorText or inspect it with the debugger.