连接三个 NSString

发布于 2024-12-06 07:33:06 字数 960 浏览 0 评论 0原文

对了,我有三个 NSString,我想将它们组合在一起形成一个长 nsstring(用作 URL)。我使用了 stringByAppedingString,它可以让我将其中两个放在一起,但我不知道如何将三个放在一起。基本上我想要最终得到的是 http://graph.facebook.com/517418970/ picture?type=large 但我需要它们在三个单独的组件中,这样我就可以更改 URl 中的数字

@implementation FacebookPicturesViewController

- (IBAction) nextImagePush {

    NSString *prefix = @"http://graph.facebook.com/";
    NSString *profileId = @"517418970";
    NSString *suffix = @"/picture?type=large";
    NSString *url = [prefix stringByAppendingString:suffix];
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
    [imageView setImage:img];
    imageCount++;


    if (imageCount >= [imageArray count]){
        imageCount = 0;
    }
}

- (void) viewDidLoad {
    imageArray = [[NSArray alloc] initWithObjects:@"image1", @"image2", nil];
    imageCount = 0;
}

Right i have three NSStrings that i want to put together to make one long nsstring (to use as a URL). I have used stringByAppedingString which lets me put two of the together but i do not know how to put three together. Basically what i want to end up with is http://graph.facebook.com/517418970/picture?type=large but i need them in three separate components so i can change the number in the URl

@implementation FacebookPicturesViewController

- (IBAction) nextImagePush {

    NSString *prefix = @"http://graph.facebook.com/";
    NSString *profileId = @"517418970";
    NSString *suffix = @"/picture?type=large";
    NSString *url = [prefix stringByAppendingString:suffix];
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
    [imageView setImage:img];
    imageCount++;


    if (imageCount >= [imageArray count]){
        imageCount = 0;
    }
}

- (void) viewDidLoad {
    imageArray = [[NSArray alloc] initWithObjects:@"image1", @"image2", nil];
    imageCount = 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千柳 2024-12-13 07:33:07

您只需两步即可完成:

NSString* partialUrl= [prefix stringByAppendingString:profileID];
NSString* fullUrl= [partialUrl stringByAppendingString:suffix];

或者,您可以使用以下格式:

NSString* url= [NSString stringWithFormat:@"%@%@%@", prefix, profileID, suffix];

You could just do it in two steps:

NSString* partialUrl= [prefix stringByAppendingString:profileID];
NSString* fullUrl= [partialUrl stringByAppendingString:suffix];

Alternatively, you could use a format:

NSString* url= [NSString stringWithFormat:@"%@%@%@", prefix, profileID, suffix];
全部不再 2024-12-13 07:33:07

作为当您事先不知道需要组合多少个字符串时的通用解决方案,您可以将它们放入 NSArray 中并使用该数组将它们连接在一起。所以在这种情况下:

NSArray *elementsInURL = [NSArray arrayWithObjects:prefix, profileID, suffix, nil];
NSString *combined = [elementsInURL componentsJoinedByString:@""];

As a general solution for when you don't know ahead of time just how many strings you have to combine, you can stick them in an NSArray and use the array to join them together. So in this case:

NSArray *elementsInURL = [NSArray arrayWithObjects:prefix, profileID, suffix, nil];
NSString *combined = [elementsInURL componentsJoinedByString:@""];
烛影斜 2024-12-13 07:33:07

您想要 + (id)stringWithFormat:(NSString *)format, ...

完整文档为 此处

You want + (id)stringWithFormat:(NSString *)format, …

Full docs are here

楠木可依 2024-12-13 07:33:07

我会像这样处理串联:

NSString *prefix = @"http://graph.facebook.com/";
NSString *profileId = @"517418970";
NSString *suffix = @"/picture?type=large";

NSString *URL =  [[prefix stringByAppendingString:profileId] 
                          stringByAppendingString:suffix];

I'd handle the concatenation like this:

NSString *prefix = @"http://graph.facebook.com/";
NSString *profileId = @"517418970";
NSString *suffix = @"/picture?type=large";

NSString *URL =  [[prefix stringByAppendingString:profileId] 
                          stringByAppendingString:suffix];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文