Get String from TextBox and compare - objective-c

I'm trying something like my first comparison App in Obj-C and i'm already running into trouble.
Well, there is a textBox with unamebox:(id)unb and a textfield NSTextField* myOut;
Well, here was my first try:
if ([unb stringValue] == #"hello") {
[myOut setStringValue:(NSString *)#"hello dude"];
}
else {
[myOut setStringValue:(NSString *)#"What?"];
}
To my shame, this always setzt the text field to "What?"
When I try the isEqualtoString, it doesn't even do anything:
if ([unb isEqualToString:(NSString*)#"hello"]) {
[myOut setStringValue:(NSString *)#"hello dude"];
}
else {
[myOut setStringValue:(NSString *)#"What?"];
}
So, what shall I do to compare it?
by the way, I already read the links which were suggested above. If I missed anything important, I'm sorry

-isEqualToString: is a method on an NSString, not on an NSTextField. You should be getting an error from sending that message.
You want this:
[[unb stringValue] isEqualToString:#"hello"]

Related

why can't I compare a string to a string literal?

I have some code that's downloading some strings from a JSON source. One of these is a login success message:
{
login = SUCCESS;
msgSuccess = "User has been logged-in successfully.";
}
So to test this, I did...
if ([[theDictionary objectForKey:#"login"] isEqualToString: #"SUCCESS"]) {
That always fails. But if I do this...
#define kLoginSuccessMessage #"SUCCESS"
if ([[theDictionary objectForKey:#"login"] isEqualToString: kLoginSuccessMessage]) {
it works perfectly.
I've never understood why. Especially when you consider that the objectForKey doesn't seem to have any problem. Can someone explain this so I don't pull out what little is left of my hair?

Assert with string argument not working as expected

EDIT: The issue was with the assert as people pointed out below. Thanks for the help!
I have a enum set that i'm trying equate, but for some reason its not working.
Its declared like so:
typedef NS_ENUM(NSUInteger, ExUnitTypes) {
kuNilWorkUnit,
kuDistanceInMeters,
//end
kuUndefined
};
And i'm using it here:
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
if (exUnit == kuNilWorkUnit)
{
assert("error with units");
}
///.... more stuff
}
Xcode isnt triggering my assert. EDIT: the assert is just for testing. i've used NSLog as well. The conditional isn't evaluating to true even though the value is clearly kuNilWorkUnit.
Does anyone have any suggestions or ideas of what i'm doing wrong?
You want to do this:
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
assert(exUnit != kuNilWorkUnit);
///.... more stuff
}
This is because, assert only stops execution if the expression you pass to it is false. Since a string literal is always non-zero, it will never stop execution.
Now, since you are using Objective C and it also looks like you want to have a message associated with your assert, NSAssert would be preferable.
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
NSAssert(exUnit != kuNilWorkUnit, #"error with units");
///.... more stuff
}

Objective C error Thread stops running

I am trying to make my code run, but it always stops.
Can someone of you help me solve the problem.
For some reason it wont accept this.
-(Animal *) getAnimalAt:(int)input {
//NSLog(#"show input %ld", input);
Animal *ani = [animals objectAtIndex:input];
return ani;
}
I call this method in my main by :
for(int i=0;i< count;i++){
Animal *ani = [farm getAnimalAt:i];
NSLog(#"ani : %#",[ani makeSound]);
NSLog(#"ani : %#",[ani doFly]);
}
If you need any more info or code please ask.
Also do any of you have found a good tutorial? I cant seem to find one?
Or a site like codingbat would be very helpfull.
If animals is just an NSArray you could remove the getAnimalAt: method and just use the NSArray. Then you could do something like:
for (Animal *ani in animals) {
NSLog(#"ani : %#", [ani makeSound]);
NSLog(#"ani : %#", [ani doFly]);
}
which will prevent any problems with count being greater than the number of elements in animals

indexOfObject does not match correctly

I've been stuck with this problem from a couple of days and I can't get myself out of it.
I've searched all over the net, but I couldn't find anything useful to solve my issue.
this is the scenario.
I've got an array of strings containing a bunch of ids fetched from a coredata sqlite db and
I'd like to know the index of a certain element into this array.
My first solution would have been as easy as using indexOfObject
-(NSInteger) getPageId:(NSString *)symbol_id {
NSInteger refId = [myIds indexOfObject:symbol_id];
// .. stuff ..
return refId;
}
now, I don't know why, but the returning value of the function is always NSNotFound.
If I print out the values via NSLog
NSLog(#"%#\n%#", myIds, symbol_id);
I can clearly see that the value I'm searching for figures out into the elements of the array.
I've even tried a dumbest solution, like probing the match via isEqual function into a for loop:
int idx = 0;
for(NSString *tok in myIds) {
if([tok isEqual:synmbol_id])
{
NSLog(#"yay, a match was encountered!!");
return idx;
}
idx++;
}
but the execution never gets into the NSLog.
I dunno where to knock my head.
hope that some of you already figured this out and could explain this to me.
thx in advance
k
Try printing all the elements on the array like this:
for(NSString *tok in myIds) {
NSLog(#"On the array [%#]", tok);
}
Maybe there is a TAB \t, an ENTER \n or something weird in your NSString preventing isEqual message to run as expected. Usually these characters are hard to find on a regular debugger. That's why I'am suggesting to enclose the string in [].

How can I configure Xcode to put '{' where I want it in generated files

I know this is a fairly contentious issue amongst programmers, but when developing I like my IDE to position the opening curly bracket underneath the method/interface/control declaration, for illustrative purposes: -
This is how Xcode automatically generates skeleton methods with the { at the end: -
-(void) isTrue:(BOOL)input {
if(input) {
return YES;
}
else {
return NO;
}
}
This is how I like to lay out my code (which I believe is called the Allman style): -
-(void) isTrue:(BOOL)input
{
if(input)
{
return YES;
}
else
{
return NO;
}
}
I'm just wondering if there's any configuration switch in Xcode to enable this style of development? It's really annoying when typing out if/else statements as it tends to auto-complete the else clause with the { at the end of the line which just looks silly if you like developing with them underneath.
Or am I being unreasonable? Is Objective-C supposed to adhere to a standard defined by Apple?
Take a look at:
Xcode: Adjusting indentation of auto-generated braces?
Apple Xcode User Defaults
XCCodeSenseFormattingOptions = {
BlockSeparator = "\\n";
PreMethodDeclSpacing = "";
};
This should at least solve your problem after if, for, or while statements.
After digesting the helpful information from WhirlWind above (thanks), the resulting snippet (just cut and paste into terminal) is:
defaults write com.apple.Xcode
XCCodeSenseFormattingOptions -dict
BlockSeparator "\\n"
PreMethodDeclSpacing ""
Stupid backslash quoting. When typed at the terminal, there should be TWO exactly TWO backslashes in the block separator.
Even with those settings, it does not appear to work with the templates. If you set this and then type "init" in a .m file you get:
- (id)init
{
self = [super init];
if (self) {
<#initializations#>
}
return self;
}
Note the "if (self) {" line.
I believe that "defaults write com.apple.Xcode" doesn't work on the latest versions of Xcode (7.x)
Here are the solutions I know:
Snippet Edit -- this little program will allow to edit default Xcode's code snippets. So, you will be able to open braces from new line in your if, for, while, etc. However, this doesn't allow to change the block indentation.
Uncrustify -- this might solve your problem too, but it doesn't look like being easy to set up. And it only formats the code after it is already written, instead of formatting 'on the go'.