Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

GenNovE

macrumors member
Original poster
Sep 21, 2008
87
0
I am just starting out so bare with my ignorance please.
I know my current issue is not hard to figure out to experienced devs. But
for a person like me that is just now learning it is very difficult.

Following the stanford dev program I am stuck on working with paths.

I need to create a NSString found the correct path method but I am stuck @ trying to expand it.

NSString *path =@"~";
Is it supposed to be like */users/frank =@"~";

I have tried diffrent methods to tell the system that ~=/users/frank.

What would be the proper way to write such path on xcode?

I have searched online and found a few helpful blogs that explain how
cooca work with strings etc.

 
I'm not 100% sure, I've just started also:

NSString *path /users/frank =@"~";

I think this is correct
 
Fail.

The method you need is stringByExpandingTildeInPath.

See here

caveman_uk

I figured out the method by simply looking at the method. It is self explanatory
but the problem I am having is how to properly write the argument.
On my search for an answer I did stumble over the same link you provided
and followed the format but I keep receiving errors.

Can you show me how to properly code it?
That way I can have a better understanding... of course that's if you wish.
If you dont no worries.
 
On my search for an answer I did stumble over the same link you provided
and followed the format but I keep receiving errors.

If you post the code as you wrote it and the exact error you are getting, somebody will probably help.
 
I tried the following.

NSString */Users/Frank = [@"~/Desktop" stringByExpandingTildeInPath];

NSString *~ = [@"/Users/Frank" stringByExpandingTildeInPath];

NSString *path /Users/Frank = [@"~" stringByExpandingTildeInPath];


They all return error messages I am @ work right now so unfortunately I don't have the actual error codes.
Will post the m later.
 
OK, so stringByExpandingTildeInPath is an instance method (on NSString) that returns an NSString *.

So you want to assign that NSString* to a variable. So this is just like anything else in C or most other programming languages. On the left side of the equals sign you want a variable. And to that variable you are assigning the result from the right side of the equals.

So it's:

Code:
int a = 2 + 2;

So the left side is a variable 'a' which is declared as type 'int'. And the right side is an expression that evaluates to 4.

So it's just the same with this:

Code:
NSString* path = [@"~/somefile.txt" stringByExpandingTildeInPath];

Again, on the left side you have a variable called 'path' which is declared as a pointer to NSString. And then on the right you have an expression that evaluates to an NSString*.

In this case the expression is a call to an instance method of NSString.

Hope that helps. At the risk of sounding like a broken record, I'm one of those who recommends that you learn some basic C first, then some Objective C, and only then try your hand at Cocoa. I think you'll find that approach much more efficient, less frustrating, and more rewarding.

Once you know things like variable assignment (C) and method calling (ObjC), then things like '- [someString stringByExpandingTildeInPath]' (Cocoa) become really easy to read in the docs and apply immediately.

Otherwise it's a bit like trying to read the newspaper without first understanding what letters and words are.
 
OK, so stringByExpandingTildeInPath is an instance method (on NSString) that returns an NSString *.

So you want to assign that NSString* to a variable. So this is just like anything else in C or most other programming languages. On the left side of the equals sign you want a variable. And to that variable you are assigning the result from the right side of the equals.

So it's:

Code:
int a = 2 + 2;

So the left side is a variable 'a' which is declared as type 'int'. And the right side is an expression that evaluates to 4.

So it's just the same with this:

Code:
NSString* path = [@"~/somefile.txt" stringByExpandingTildeInPath];

Again, on the left side you have a variable called 'path' which is declared as a pointer to NSString. And then on the right you have an expression that evaluates to an NSString*.

In this case the expression is a call to an instance method of NSString.

Hope that helps. At the risk of sounding like a broken record, I'm one of those who recommends that you learn some basic C first, then some Objective C, and only then try your hand at Cocoa. I think you'll find that approach much more efficient, less frustrating, and more rewarding.

Once you know things like variable assignment (C) and method calling (ObjC), then things like '- [someString stringByExpandingTildeInPath]' (Cocoa) become really easy to read in the docs and apply immediately.

Otherwise it's a bit like trying to read the newspaper without first understanding what letters and words are.

So path points to NSString & NSString the points to the method?

Look @ the below illustration Am I correct in believing that is how the code is carried out?
 

Attachments

  • PATH.JPG
    PATH.JPG
    4.2 KB · Views: 76
So path points to NSString & NSString the points to the method?

Look @ the below illustration Am I correct in believing that is how the code is carried out?

No, sorry, I didn't state that very well earlier.

'path' is a pointer to an NSString. So you are declaring that this variable holds a pointer to an object of type NSString. Remember NSString* is just a variable type, just like 'int' is a variable type. If you say 'int a' you are saying that a is a variable which holds a value of type 'int'.

If you have experience with some other programming language, let us know. Then I can explain it in terms of that language.

There is also another NSString here which is "~/somefile.txt". You are sending a message to that string and in return you are getting a pointer (of type NSString*) to another new string which is "/Users/whatever/somefile.txt".

Then you are storing that new pointer to that new string in the variable called path.

This may be more clear if you do it in two lines:

Code:
NSString* path;
path = [@"~/somefile.txt" stringByExpandingTildeInPath];
// now path hold a pointer to a new string that was just created (and autoreleased, btw)

Again, if you tell me what programming languages you have experience with, then I can explain this in those terms.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.