1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

ascii text from a URL into a string?

Discussion in 'iPhone/iPad Programming' started by jerrywickey, Apr 28, 2009.

  1. macrumors member

    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.

  2. Moderator


    Staff Member

    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).
  3. macrumors 6502a

    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:

    [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 ;)
  4. macrumors member

    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.

  5. Moderator


    Staff Member

  6. macrumors 6502a

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


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


    Holy Cow! Why do they hide that link?

    That is what i need

    Thank you.

  8. macrumors 603

    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.

  9. Moderator


    Staff Member

    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.
  10. macrumors member

    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.


  11. Moderator


    Staff Member

    No, stringWithContentsOfURL is a static method of the NSString class. So you would need to do something like this:
    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.
  12. macrumors member


    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

  13. Moderator


    Staff Member

    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.
  14. Moderator emeritus


    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:
    #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.

    What questions do you have? Have you read through any OOP tutorials?
  15. macrumors member

    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?

  16. Moderator


    Staff Member

    ...so you think. I suspect you'll be back. With further questions.

    It is.

    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.
  17. macrumors member

    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.


  18. Moderator


    Staff Member

    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).
  19. macrumors member

    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

    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.

  20. Moderator


    Staff Member

    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.

    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.
  21. macrumors member

    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.


    testingViewController.h file

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

    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];
  22. Moderator emeritus


    @"...." 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.
  23. macrumors member

    beautiful kain,

    Thank you


    contrary to popular opinion I am only dumb not stupid.
  24. macrumors member

    Just changed it.

    Worked perfectly.

    Thanks again.


    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.
  25. Moderator


    Staff Member

    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.

Share This Page