This question already has answers here:
Comparing Strings in Cocoa
(5 answers)
Compare two NSStrings
(3 answers)
Closed 10 years ago.
I am having some very frustrating trouble on what I'm sure is a very simple problem, but I cannot seem to fix it. I have an NSArray called final that outputs as follows:
final = (
".DS_Store",
"hey.txt"
)
I want the following for loop to return false for the first pass and true for the second. As far as I can tell I have it made correctly but the output is true for both passes.
for (int i = 0; i < [final count]; i++) {
if (final[i] != #".DS_Store") {
NSLog(#"true");
}
else {
NSLog(#"false");
}
Outputs:
2013-02-20 17:20:39.042 myAppName [40636:403] true
2013-02-20 17:20:39.042 myAppName [40636:403] true
I cannot figure out why the first one does not return false. Any Ideas?
You are comparing pointers. Use [final[i] isEqualToString:#".DS_Store"] to compare strings.
Related
This question already has answers here:
2D Array in Kotlin
(11 answers)
Closed 1 year ago.
I am genuinely shocked on how hard it is to find a good explanation on how to create a 2d array in Kotlin for an object.
These are my attempts from what I have found neither here on stack and online neither work, why? how do I create a 2d array of objects not built into Kotlin!!!
var matrix : Array<Array<myObject?>> = null
//var arr2D = Array(10) { Array(10) { myObject(this) } }
for (i in 0 until 9) {
for (j in 0 until 9) {
matrix[i][j] = myObject(this)
}
}
It says "null can not be a value of a non-null type" so I guess I have to use an arrayofnulls(), but cannot find a source can someone help me or give me a source?
This is how you create a 2D Array in Kotlin with a user made object. ArrayofNulls allows you to set all indexes in the array to null and then just initialize them later with a for loop!
val matrix = Array(10) {
arrayOfNulls<myObject?>(
10
)
}
This question already has answers here:
Removing from array during enumeration in Swift?
(9 answers)
Closed 6 years ago.
In this loop we have the potential to reduce the number of items in the loop while processing it. This code works fine in Obj-C, but the Swift loops don't get the message that an item has been removed and end up overflowing the array.
In Objective-C, we had:
for(int i = 4; i < staticBlocks.count; i++)
{
PlayerSprite* spr = [staticBlocks objectAtIndex:i];
[spr setPosition:CGPointMake(spr.position.x, spr.position.y-1)];
if(spr.position.y < -1000)
{
[staticBlocks removeObject:spr];
[spr removeFromParent];
}
if(spr.blockTypeIndex == Block_Type_Power_Up)
{
[spr update];
}
}
In Swift I know of these options:
//for i in 4.stride(to: staticBlocks.count, by: 1){ //crashes
//for i in 4..<staticBlocks.count{ //crashes
for var i = 4; i < staticBlocks.count; i += 1 { //works, but is deprecated
let spr = staticBlocks.objectAtIndex(i) as! PlayerSprite
spr.position = CGPointMake(spr.position.x, spr.position.y-1)
if(spr.position.y < -1000)
{
staticBlocks.removeObject(spr)
spr.removeFromParent()
//break
}
if(spr.blockTypeIndex == k.BlockType.PowerUp)
{
spr.update()
}
}
In this specific case, it really isn't a problem for me to use a break statement (which is currently commented out) to kill the loop and prevent the crash, but it doesn't seem like the proper fix. I assume there will come a time when I need to know how do do this correctly. Is there a non deprecated way to do a for loop, one which processes the count each pass?
A related, unanswered question.
Is the for loop condition evalutaed each loop in swift?
I don't know how to link to a specific answer, but this code did what I needed. Marking as duplicate now.
Removing from array during enumeration in Swift?
var a = [1,2,3,4,5,6]
for (i,num) in a.enumerate().reverse() {
a.removeAtIndex(i)
}
This is because in Swift you cannot remove items from an array while you are iterating over it.
From this question Removing from array during enumeration in Swift?
you can see that you should be using the filter function instead of using a for loop.
For example, don't do this:
for (index, aString: String) in enumerate(array) {
//Some of the strings...
array.removeAtIndex(index)
}
Do something like this:
var theStrings = ["foo", "bar", "zxy"]
// Filter only strings that begins with "b"
theStrings = theStrings.filter { $0.hasPrefix("b") }
(Code example from this answer)
Additionally, it should be noted that filter won't update the array, it will return a new one. You can set your array to be equal to that array afterwards.
An additional way to solve the issue, from the same question is to do this:
var a = [1,2,3,4,5,6]
for (i,num) in a.enumerate().reverse() {
a.removeAtIndex(i)
}
print(a)
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
So im trying to code a function to remove an element from an Array.for some reason i'm getting no errors but still does not print the result i need. i think the problem is in the function or data type declaration.
#import <Foundation/Foundation.h>
void deleteArray(char stra[ ], char ElementToRemove);
int main(int argc, const char * argv[]) {
#autoreleasepool {
char str[100];
printf("Please Enter Array Elements\n");
scanf("%s",&str);
deleteArray(str, "a");
printf("%s",&str);
}
return 0;
}
void deleteArray(char stra[ ], char ElementToRemove)
{
int NumberOfElements = sizeof(stra);
int ElementPos;
for (int i = 0; i >= NumberOfElements;i++)
{
if (ElementToRemove == stra [i])
{
ElementPos = i;
}
}
for (int SecondCounter = ElementPos; SecondCounter >= NumberOfElements;SecondCounter++ )
{
stra[SecondCounter] = stra[SecondCounter - 1];
}
}
There are many issues with your code, let's see them one by one.
When you pass an array to a function, it decays to a pointer to the first element of the array. So, sizeof in the deleteArray() function is not doing what you think it's doing there.
You can use strlen() instead to get the length of a char array. However, please note, this does not count the terminating null, anyways, and you need to move that one, too, to make the end of the modified array.
Then, in the for loop,
for (int i = 0; i >= NumberOfElements;i++) //false always....
is wrong. I believe what you want is
for (int i = 0; i < NumberOfElements;i++)
After that, regarding the call to the function should be
deleteArray(str, 'a'); // 'a' is a char
instead of
deleteArray(str, "a"); // "a" denotes a string
Next, in the main() function, remove the & from the argument to printf(). It should look like
printf("%s",str);
Also, to ensure safety from buffer overflow, you should make yourscanf() to look like
scanf("%99s",str);
If you need dynamically sized arrays, I recommend making them the last flexible array member (that wikipage has an example) of some (growable) struct and keep the size of that array in its containing struct ....
If you want to have array features, such as add, delete, move and so on, use linked lists. Linked lists are using pointers, so you can represent an array using them. This way it is possible to delete an element, move it or add a new one.
When declaring an array in c, you declare it fixed size. In your case, if you want not to use pointers and lists, you have to copy array elements to a new one, excluding the unneeded.
This question already has answers here:
Objective C - XCode not recognizing variable outside of if statement
(2 answers)
Closed 8 years ago.
New in OBJ-c,
Im tring to set a varible with some if statement and it's not work.
if (_imageView,image==NULL) {
NSString *isFileSet = #"NOFILE";
}
else
{
NSString *isFileSet = #"FILESET";
}
NSLog(#"%#",isFileSet);
and I cant run the project, I get an err: "Use of undeclared identifier "isFileSet".
what is worng here ?
That's because you declared the variables in the if block. The variable won't exist outside of it (true for all programming languages I can think of). You should have declared it outside:
NSString *isFileSet;
if (_imageView.image==NULL) {
isFileSet = #"NOFILE";
}
else
{
isFileSet = #"FILESET";
}
NSLog(#"%#",isFileSet);
You should have also fixed the if-statement.
This question already has answers here:
Create multiple numbered variables based on a int
(2 answers)
Closed 8 years ago.
I'm using
for(int k=i;k<6;k++){
int q=k+1;
switch (q) {
case 1:
textbox1.hidden=YES;
break;
case 2:
textbox2.hidden=YES;
break;
case 3:
textbox3.hidden=YES;
break;
case 4:
textbox4.hidden=YES;
break;
case 5:
textbox5.hidden=YES;
break;
case 6:
textbox6.hidden=YES;
break;
default:
textbox1.hidden=NO;
break;
}
}
I was wondering if there isn't anyway to use make something like this:
[#"textbox%#.hidden] = YES
or something like that..
The second question
I have to do something likes this:
[textbox2 setKeyboardType:UIKeyboardTypeDecimalPad];
But since Im on a for .. I can't put textbox2 I need to put "textbox%#, i" So it can detect on which textbox it is analyzing any idea?
You cannot make the replacement you're asking about. It's probably possible to figure out some alternative using reflection but the resulting code will be much uglier than what you've already got. You could, however, take advantage of your q being 1-6 and use an array of text boxes:
id textboxes[] = {textbox1, textbox2, ... textbox6};
if ((q >= 1) && (q <= 6)) textboxes[q-1].hidden = YES;
else textbox1.hidden=NO;