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

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
Just a newbie question. Still trying to get a handle on C++ syntax

I need to fill a string with the contents of a myURL. the file is 16,000 characters of ascii text.

1 ) Can I use?

+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error


2) Do I need to import NNString into the header file of the implementation file in which I use this?


3) Can I simply declare the sting variable into which to place the contents of the url like this?

chr textIAmUsing(16000);


4) Would the statement which fills textIAmUsing with the contents of the myURL be?

textIAmUsing = (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error


5) Can the text from above "(NSURL *)url" be replaced simply with http://www.pagefulloftext.txt ) ?

and can (NSStringEncoding)enc be replaced simply with 1 ? because 1 is the NSASCIIStringEncoding

Thanks for any answers.

Jerry
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Given your post I think you need to go and learn basic Objective-C before attempting to write any real code.

Once you've done that you'll know the answers to 2-5 (and see the errors on every line).
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
Executive summary: buy a book on the basics, read the documentation for class references.

1. You can, but you're better looking at NSURLConnection and threading so you don't block the main thread (it runs the UI, so blocking it blocks user interaction).

2. No, by default your prefix file (App_Prefix.pch) will import Foundation and UIKit into everything automatically.

3. Take a look at the NSString class reference.

4. No. stringWithContentsOfURL::: is a class method, so you call it with:

Code:
[NSString stringWithContentsOfURL:yourURL encoding:NSASCIIStringEncoding error:&error];

I recommend buying a book on Objective-C in order to get syntax basics, otherwise you'll never get anywhere.

5. No, see the NSURL Class Reference.

You could replace NSASCIIStringEncoding with 1 and it would still work, but that somewhat defeats the purpose of helpfully-named constants ;)
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
C++ syntax is at the heart of my questions.

I google it. I look around for it. It is always "If you want to do this, type this." or explaining how important OOP is. Never, "here are the six or whatever syntaxes for passing messages", "here is a list of methods and their class, in which file to declare, import, or whatever them and here is a list of arguments they take, by the way, to get an argument to a method, you may need another method of another class, here it is."

Could you give me such a link.

By the way to learn this I am taking one of my applications and converting it right now. I have three more specific things to learn and it will be done.

Jerry
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
C++ syntax is at the heart of my questions.

I google it. I look around for it. It is always "If you want to do this, type this." or explaining how important OOP is. Never, "here are the six or whatever syntaxes for passing messages", "here is a list of methods and their class, in which file to declare, import, or whatever them and here is a list of arguments they take, by the way, to get an argument to a method, you may need another method of another class, here it is."

Could you give me such a link.

By the way to learn this I am taking one of my applications and converting it right now. I have three more specific things to learn and it will be done.

Jerry

You want this then: http://cocoadevcentral.com/d/learn_objectivec/
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
Robbie,

Thank you for the infor. but I already did complete my first IPhone app and the code examples were lousy.

JNIC,

Holy Cow! Why do they hide that link?

That is what i need

Thank you.

Jerry
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
3) Can I simply declare the sting variable into which to place the contents of the url like this?

chr textIAmUsing(16000);

Yes you can. You'd either have to copy the data to/from an NS object. Or seriously hack up that object. The reason you don't want to copy/hack is that Apple has written a gazillion subroutines that work with whatever bizarre location that the NS object stores the actual *char bits, and if you want to leverage all their code, you let it handle it's potentially bizarre random location for the bits, and try hard to forget about snarfing the pointer trail and hacking on the struct bits directly.

imho.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Robbie,

Thank you for the infor. but I already did complete my first IPhone app and the code examples were lousy.

Well you clearly did so without understanding the basic syntax of Objective-C (based on your questions above) which is why I suggest you read the language guide and actually understand what you are doing. I can only think that you found the code examples lousy as they did not enable you to copy and paste exactly what you needed without writing any code yourself; I found them to be an excellent guide to the correct way to do things and very helpful.
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
This is the answer to my question. but it brings up another.

dnadata = [stringWithContentsOfURL:[NSURL URLWithString:mad:"http://www.satellitemagnet.com/mtdump.txt"] encoding: 1 error: NULL];

On Build I get the error "stringWithContentsOfURL undeclared, first use in this function"

Obviously I need to declare something. stringWithContentsOfURL is an instance of the class NSObjects, right?

When I attempt #import <NSObjects> I get no such file name.

None of the docs say "when using stringWithContentsOfURL you must import blah blah.

Does any one know what I must import. Or perhaps I made an another mistake.

If someone could simply say

Import x in the header file or in the implementation file.

Thanks

Jerry
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
dnadata = [stringWithContentsOfURL:[NSURL URLWithString:mad:"http://www.satellitemagnet.com/mtdump.txt"] encoding: 1 error: NULL];

On Build I get the error "stringWithContentsOfURL undeclared, first use in this function"

Obviously I need to declare something. stringWithContentsOfURL is an instance of the class NSObjects, right?
No, stringWithContentsOfURL is a static method of the NSString class. So you would need to do something like this:
Code:
dnadata = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.satellitemagnet.com/mtdump.txt"] encoding: 1 error:  NULL];
P.S. You also shouldn't be using 1 for the encoding parameter (use constants instead) or NULL for the error parameter (nil is recommended).

As others have suggested, it is time for you to step back and learn the basics of Objective-C coding before you continue any real coding. Otherwise, you are going to continue to struggle.
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
dejo

I do need a static string not a mutable. The data will not be changed at all. And static will likely be faster and use fewer resources.

Ok thats good using a constant instead of "1" but it will only make a differenet in the source code.

and passing the stringWithContentsOfURL to nsstring worked perfectly.

Of course, stringWithContentsOfURL is a method of NSString.


Now why can't everyone answer this effectively. Contrary to what most might think, I am a very accomplished programmer. If someone can't explain OOP to me by simply answering my questions, then either they don't really understand it or OOP is capricious and arbitrary.

There exists those who can program but do not understand what they do.

Thank you dejo



Jerry
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
dejo

I do need a static string not a mutable. The data will not be changed at all. And static will likely be faster and use fewer resources.

You were told the answer above and have wilfully misunderstood it. It's nothing about the string being static. It's a class (static) method not an instance method.

Go learn the language. You are making very basic conceptual errors and clearly have no idea what you are doing.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
When I attempt #import <NSObjects> I get no such file name.

None of the docs say "when using stringWithContentsOfURL you must import blah blah.

Does any one know what I must import. Or perhaps I made an another mistake.

If someone could simply say

Import x in the header file or in the implementation file.
When working with Cocoa you don't need to import headers for every class, you can just import a single header. Most of the time all you'll need for the iPhone is:
Code:
#import <UIKit/UIKit.h>
This file will import every header in UIKit, along with the Foundation framework. So don't worry about imports until you're dealing with a class that is outside of UIKit/Foundation frameworks.

Now why can't everyone answer this effectively. Contrary to what most might think, I am a very accomplished programmer. If someone can't explain OOP to me by simply answering my questions, then either they don't really understand it or OOP is capricious and arbitrary.

What questions do you have? Have you read through any OOP tutorials?
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
I just have some simple questions and as I get the answers i will be fine.

the line dejo gave me compiled perfectly but...

dnadata = [NSString stringWithContentsOfURL:[NSURL URLWithString:mad:"http://www.satellitemagnet.com/mtdump.txt"] encoding: 1 error: NULL];

NSString *dnadata; is declared such. It is called declared? Isn't it?

I have no problems passing strings to dnadata and verifying the content simply by placing the content in a label and checking

lookdna.text = dnadata;

If I place some other string in dnadata it appears in the label. When I try to place the contents of the file mtdump.txt into dnadata by the line above, there are no compile errors but dnadata does not contain any text from the file.

hovering over dnadata I get the value invalid while all the other variables contain what I expect in them.

What dumb thing could I be doing?

Jerry
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I just have some simple questions and as I get the answers i will be fine.
...so you think. I suspect you'll be back. With further questions.

NSString *dnadata; is declared such. It is called declared? Isn't it?
It is.

When I try to place the contents of the file mtdump.txt into dnadata by the line above, there are no compile errors but dnadata does not contain any text from the file.

hovering over dnadata I get the value invalid while all the other variables contain what I expect in them.

What dumb thing could I be doing?
First, I went to that URL and got a 404 Error, so you're trying to pull data that doesn't exist. Second, you may want to consider setting up an NSError variable and passing a pointer to that as the error paramter of the stringWithContentsOfURL method. Then if there is an error, you can examine the contents of that variable to see what went wrong. It's a technique known as exception handling.
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
the file is there. You got 404 because I renamed it and attempted to load the new name just incase there was a problem with caching or something.

the new name is http://www.satellitemagnet.com/mtdump2.txt

It is there. but since you don't see any obvious errors, or you would have pointed them out, what is going on.

Thanks

Jerry
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
but since you don't see any obvious errors, or you would have pointed them out, what is going on.
I don't see any obvious errors but I also don't have all the code in front of me. You do. So, now is an opportunity to learn how to troubleshoot problem code. I already explained one way to do it (hint: NSError).
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
There isn't any other code involved.

NSString *dnadata; in the .h file

dnadata = [NSString stringWithContentsOfURL:[NSURL URLWithString:mad:"http://www.satellitemagnet.com/mtdump.txt"] encoding: 1 error: NULL];

in the .m file under an IBAction button

then
lookdna.text = dnadata;

I Know everything else is working because when I replace the dnadata = line with dnadata = @"This is test data"; it works just fine. The test data line appears in the label.

But if I use the dnadata = [NNS... line nothing appears

I have already renamed the file and I have reduced the files size. Same results.

It could be the encoding. The file contains single byte ascii text. What exactly is NSASCIIStringEncoding = 1 (from the docs) is it single byte ascii?

Another problem it could be is something hinky in the web connection. I have another html file that won't load from the IPhone simulator but will from Safari. It did load in the IPhone sim but won't now. A copy of the file with a different name (1 added to the end) will load in the iphone sim. I rebooted the machine and cleared the cach but no luck. I am using the different name and I have copied the dnadata file as well with a new name.

Is there any where in the documentation that NSerror codes would be enumerated and explained? The docs seem to have a problem with the simple stuff like that.

Jerry
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
There isn't any other code involved.
Obviously there is. You are not providing us anything other than snippets. Also, I'd suggest you enclose your code in [ CODE ] tags to avoid potential formatting issues.

Is there any where in the documentation that NSerror codes would be enumerated and explained? The docs seem to have a problem with the simple stuff like that.
I'd venture to say that because you don't understand the fundamentals, the problem is not with the docs but with your interpretation of them.
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
It is really pretty simple:

This is it. Am I missing anything? Should I have put all of this.

This is the whole project. And if you don't beleive me, I would be glad to send you the files

How can the line above the //OR work but not the line below. Unless of course that line is wrong.

Jerry


testingViewController.h file

#import <UIKit/UIKit.h>
NSString *dnadata;
@interface testingViewController : UIViewController {
IBOutlet UILabel *lookdna;
}
-(IBAction) play:(id) sender;
@property (nonatomic, retain) UILabel *lookdna;
@end


testingViewController.m file

#import "testingViewController.h"
@implementation testingViewController
@synthesize lookdna;
- (void)viewDidLoad {
// dnadata = @"this is text";
// OR
// dnadata = [NSString stringWithContentsOfURL:[NSURL URLWithString:mad:"http://www.satellitemagnet.com/mtdump5.txt"] encoding: 1 error: NULL];
}
- (IBAction)play:(id)sender{
lookdna.text = dnadata;
}
- (void)dealloc {
[super dealloc];
}
@end
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
@"...." is a constant NSString and is different than normal NSString objects created at runtime. Constant strings do not get deallocated, while normal NSString objects will if it is an autoreleased object, and that is what you are creating, which is why it's not working because by the time you click the button the string is autoreleased. Therefore you need to retain dnadata after creating it. If you do not understand this you should read up on memory management.

And like dejo said above, use NSASCIIStringEncoding instead of 1. This is referred to as an enumerated type.
 

jerrywickey

macrumors member
Original poster
Apr 16, 2009
81
0
Key West
Just changed it.

Worked perfectly.

Thanks again.

Jerry

I've been at this IPhone thing now for almost 12 hours over 2 weeks. It is my first foray into OOP programming. All you young'uns should give an old assembler guy a break.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
All you young'uns should give an old assembler guy a break.
Who you calling a young'un? :)

I used to program 6502 assembler back in the day. But that was a long time ago.

I hope you don't think I was giving you too hard a time. I have been trying, along with other posters here, to guide you in a direction that we believe will be the path of your success. The whole "teach a man to fish" approach.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.