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 [].
Related
I'd like to refer variable which I define inside of if block at outside of if else block. How can I? If it can't be, do I have to write same long code (they can't be function or method) inside if block and else block?
if (aTrack.compilation)
{
NSString *artistName = NSLocalizedString(#"compilations", #"");
}
else
{
NSString *artistName = aTrack.artist
}
NSLog(#"%#",artistName);
What #nickfalk and #CRD posted is good but you can also (for such easy statements) use a ternary operator which in this case will look like this:
NSString *artistName = aTrack.compilation ? NSLocalizedString(#"compilations", #""): aTrack.artist;
NSLog(#"%#",artistName);
It is a matter of style but I would go this way for this simple example as my code is in one line
The lifetime of an ordinary (non static) variable declared in a block is just that block, any nested blocks, etc. This is part of the standard lifetime and visibility rules of (Objective-)C(++).
Just declare the variable before the if/else and assign values to it in each block.
This will do what you want:
NSString *artistName;
if (aTrack.compilation){
artistName = NSLocalizedString(#"compilations", #"");
} else {
artistName = aTrack.artist;
}
NSLog(#"%#",artistName);
Also have a look at CRD's reply as this is really basic knowledge and you really need to understand this. (Also, as viperking noticed in my example, there was a terminating semicolon missing in your original code...)
Viperking has a nice example using the the ternary operator. It might be a bit alien at first but is rather nice when you wrap your head around it. a third solution would be
NSString *artistName = aTrack.artist;
if (aTrack.compilation){
artistName = NSLocalizedString(#"compilations", #"");
}
NSLog(#"%#",artistName);
For more complex scenarios I would advice against it, but for an example with two possible cases and one single string it would be quite legible. I'd also advice using nil rather than an empty-string for the localizedString comment.
I have two objective c methods. One needs to return an int[][] and the other which needs to take int[][] as a parameter. I was originally using an NSMutableArray with NSMutableArrays as values however I was told to redo it like this in order to be compatible with some current code. I can't figure out how to make this work. I'm not sure I'm even googling the right thing. Anyway here is what I have now.
+(int [][consantValue]) getCoefficients
{
int coefficiennts [constantValue2][constantValue1] = { {0,1,2}, {3,4,5}, {6,7,8} };
return coefficients;
}
At the return statement I get the Error "Array initilizer must be an initializer list'
I also have to take the int[][] and rebuild it into an NSMutableArray of NSMutableArrays in another method but I'm hoping if someone can give me a hint on the first part I can work the second part out myself although if anyone has any advice on that I would appreciate it as well. Thanks.
The easy way to do this for fixed size array(s) is to use a struct for storage:
typedef struct {
int at[constantValue2][constantValue1];
} t_mon_coefficients;
And then you'd declare the method which returns by value:
+ (t_mon_coefficients)coefficients;
And passes by value as a parameter:
- (void)setCoefficients:(const t_mon_coefficients)pCoefficients;
If the struct is large, you should pass by reference:
// you'd use this like:
// t_mon_coefficients coef;
// [SomeClass getCoefficients:&coef];
+ (void)getCoefficients:(t_mon_coefficients* const)pOutCoefficients;
- (void)setCoefficients:(const t_mon_coefficients*)pCoefficients;
But there are multiple ways one could accomplish this.
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
When using NSCoding and decoding values, is there a way to tell if a value exists for a given key? In other words, what I'm trying to do is...
if([decoder valueExistsForKey:#"myKey"]) //valueExistsForKey is not a real method :(
{
NSInteger *myInt = [decoder decodeValueForKey:#"myKey"];
}
else
{
//handle special case
}
The issue is that I have old versions of documents in my app that don't have the "myKey" value, and if they don't have it, using 0 for myInt (what happens if you decode a nonexistent key) is not the behavior I want. However, I can't just decode and check if myInt == 0, because it might legitimately be equal to 0.
Since the valueExistsForKey method does not seem to exist, how can I replicate this behavior?
How about containsValueForKey?
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"]