User |
Post |
11:27 am
November 18, 2008
|
Noob
|
|
England
|
|
|
posts 5
|
|
|
Hi,
I'm still learning this Objective-C malarky and it can be quite frustrating at times….. 
However, it is starting to make sense, but I cannot get a loop to do just that!
Thanks to Brandon for his gem of a find with the Stanford University tutorials. I'm stuck on 1B, using an array. I cannot get the loop working to print out the entire array. I know it's got three elements in it. I can only get it to print out to the screen on the first pass of the loop, it doesn't seem to loop back around for the other two times.
The code has been re-checked by me a number of times, but no joy!
Here is the code;
// Main bit of code here…
NSString *path = @”~” ;
NSLog(@”My Home Folder is %@”, path.stringByExpandingTildeInPath);
// Array
// Put the components into the array
NSArray *pathComponents = [path.stringByExpandingTildeInPath pathComponents];
// Count how many there are
int *count = [pathComponents count];
// Output to check
NSLog(@”There are %d elements in my array”, count);
// Set up the Loop variables
int *i;
int *counter;
// Loop for each element in the array
for (i = 0, counter = count; i < counter; i = i + 1)
{
// Output just to check on the number in the array.
NSLog(@”Counter = %d”, count);
// Setup the variable, for each time it loops
NSString *element = [pathComponents objectAtIndex:i];
// Write it out
NSLog(@”The element at index %d in the array is: %@”, i, element);
}
[pool drain];
return 0;
Thanks in advance for your help! 
Cheers
Graham
|
|
11:47 am
November 18, 2008
|
VertigoSol
Moderator
|
|
|
|
|
posts 26
|
|
|
first of all ints need to be declared as ints and not int pointers
int* count …
// Set up the Loop variables
int *i;
int *counter;
should be
int count =…
int i, counter;
this is kind of a messy for loop to understand what is going on
if you just looping through an array use somthing like
int i;
int sizeOfArray = [pathComponents count];
for(i=0; i < sizeOfArray; i++)
{
// Output just to check on the number in the array.
NSLog(@”Counter = %d”, i);
// Setup the variable, for each time it loops
NSString *element = [pathComponents objectAtIndex:i];
// Write it out
NSLog(@”The element at index %d in the array is: %@”, i, element);
}
…
|
|
|
12:00 pm
November 18, 2008
|
Noob
|
|
England
|
|
|
posts 5
|
|
|
Hi VertigoSol,
Thanks for helping me with this, it's appreciated! 
I'm not sure what the difference is between the 'int' and the 'int pointer', but it definitely works!!
I'm going to have a look at your suggestions, but one thing that does throw me is the 'i++' bit. I know it means 'add another one', but it's looks odd….
I'm self taught in VB.Net and have done OOP in that, but it's taking a bit of time to get the Obj-C to sink in… The syntax is so different and the Visual Studio IDE is more helpful then the Xcode equivalent. The dot syntax is more difficult in Xcode.
But, I'll endeavour and I'll get there in the end!!
Once again, many thanks for your help with this!
Cheers
Graham
|
|
1:25 pm
November 18, 2008
|
VertigoSol
Moderator
|
|
|
|
|
posts 26
|
|
|
an int* type is a pointer that points to an int . pointers are mainly used in c level program they essentially hold a memory address to a value.
Don’t worry too much about using them in objective-c if you are just beginning with the language since they are complicated to understand and even harder to use correctly.
ex c code
int pointer*;
int nonPointer = 5;
pointer = &nonPointer
//pointer and nonpointer = 5
nonPointer = 7;
//pointer value also changes to 7
printf(”%d %d, nonPointer, *pointer);
… //it can get messy real fast lol
i++ is a shorthand for i=i+1
i** is shorthand for i = i*1
etc is a easy shorthand post-fix notation to increment a variable
as you probably noticed most object types NSString NSArray etc
are created with something like
NSString* myString = …create string
this is a object pointer but using it straighforward
just stay away from the pointer (*) until you understand the fundamentals and you’ll be fine (:
hope this helps
|
|
|
4:05 pm
November 18, 2008
|
Noob
|
|
England
|
|
|
posts 5
|
|
|
I'll take your advice and stay away from pointers for now! 
The only thing I understand about them at the moment, is that I don't understand them at all!! :-s
Cheers
Graham
|
|
8:28 pm
December 25, 2008
|
kevindtimm
Noob
|
|
|
|
|
posts 1
|
|
|
a lot of good info so far but a couple of clarifications:
declare:
int *pointer;
not
int pointer*;
i** does not mean i = i * 1;
It actually doesn't mean anything. int **i does mean a pointer to a pointer (which is typically used as a pointer to an array).
But, if you're coming from VB, don't even consider pointers for a VERY long time. Though they are one of the most beautiful things ever invented, misused/mishandled they will make your world go poof!
|
|