xCode Swift NSString problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    xCode Swift NSString problem

    Hi.

    I'm learning Swift and struggling with a process for obtaining an image from the web via passing a parameter to a URL.

    The code is as follows

    Code:
    NSString* myURLString = [NSString stringWithFormat:@"http://mydomain.com/uploads/%@", userimg];
    NSURL* imageURL = [NSURL URLWithString:myURLString];
    NSData* imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage* image = [UIImage imageWithData:imageData];
    self.userAvatar.image = image;
    The first line shows errors relating to 'Consecutive statements on a line must be separated by ;' yet my code seems to be as suggested by all online tutorials etc.

    Anyone either know what's wrong or how to grab an image file from a server by passing a parameter to the url?

    Many thanks and Happy New Year!
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    I don't know swift; however looking at the Apple developer library:


    I suspect your code is malformed try:

    Code:
    NSString *myURLString = [NSString stringWithFormat:@"http://mydomain.com/uploads/%@", userimg];
    NSURL *imageURL = [NSURL URLWithString:myURLString];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];
    self.userAvatar.image = image;
    What I noticed in the developer's reference library was the shift in positioning of the asterix in the script in the original post verses the scripts in the reference documentation; thus, the above script simply moves it over one space so that it matches the reference.

    >> BUYER BEWARE >> This is only a best guess on my part. I have no background in this programing language beyond a 15 minute read into the language reference manuals; thus, this will be the extent of the help I can offer you.

    One more link (also from Apple's developer library; however, I think this is also directly related to your goal>
    Reading Strings From and Writing Strings To Files and URLs
    Last edited by zmbd; Jan 2 '16, 07:32 AM.

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      Many thanks for the reply zmbd - appreciate the help despite the lack of Swift experience.

      Unfortunately the asterisk issue had been tried earlier and makes no difference - the part of the code underlined in red is the :@ as it seems to want to insert a ; in there but the line seems to be written exactly as code snippets suggest.

      Even if I paste in the examples from the documentation I have the same issue so maybe this is one of those issues where the error occurs many lines previous in the code but only flags later. Will try looking for errors in earlier lines.

      Many thanks again

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        that's unfortunate... I was really hoping it was going to be an easy fix for you! Might still be worth moving the asterix before you start combing thru all of the other lines of code...

        B.O.L.
        Happy New Year!

        -z

        Comment

        • robertybob
          New Member
          • Feb 2013
          • 116

          #5
          Nothing seems to fix this. It's a bit ridiculous as you would think this is pretty basic usage - just trying to get a specific file from the web.

          Is there really no code that works in xCode 7 Swift 2.0?

          Comment

          Working...