ShareKit API 更改

发布于 2024-12-29 15:34:53 字数 387 浏览 3 评论 0原文

我正在尝试将附件的 ShareKit API 代码更改为以下代码:

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@\",\"href\"
:\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL) 
SHKEncode(item.title),SHKEncodeURL(item.URL)];

Xcode 不断告诉我前缀错误。 我缺少什么?

I am trying to change ShareKit API code of attachment to this code below:

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@\",\"href\"
:\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL) 
SHKEncode(item.title),SHKEncodeURL(item.URL)];

Xcode Keeps telling me prefix error.
What am I missing?

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

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

发布评论

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

评论(2

瞄了个咪的 2025-01-05 15:34:54

我很遗憾地说,但是您的代码是一大段意大利面条代码。它很难阅读、容易出错并且难以维护(不仅对你来说,对你的程序员同事也是如此)。

我想建议稍微分解一下代码 - 例如:

// one way to define constant strings...
NSString * const kSHKAttachmentTemplateString = @"{\"name\":\"%@\",\"href\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\",\"href\":\"http://example.com/\"}]}"

// and another one that uses a #defined constant string at the beginning of header file of .m file
#define SHK_ATTACHMENT_TEMPLATE_STR    @"{\"name\":\"%@\",\"href\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\",\"href\":\"http://example.com/\"}]}"

// assuming item is an valid object
NSString *itemTitleOrURL = (item.title == nil) ? SHKEncodeURL(item.URL) : SHKEncode(item.title);

dialog.attachment = [NSString stringWithFormat:SHK_ATTACHMENT_TEMPLATE_STR, itemTitleOrURL, SHKEncodeURL(item.URL)];

遵循可读的代码风格肯定会节省您的时间,同时解决类似的语法错误。

I'm sorry to say, but your code is one big piece of spaghetti code. It is hard to read, prone to mistakes and hard to maintain (not only for you, but for your fellow coders as well).

I'd like to suggest breaking down the code a bit - for example:

// one way to define constant strings...
NSString * const kSHKAttachmentTemplateString = @"{\"name\":\"%@\",\"href\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\",\"href\":\"http://example.com/\"}]}"

// and another one that uses a #defined constant string at the beginning of header file of .m file
#define SHK_ATTACHMENT_TEMPLATE_STR    @"{\"name\":\"%@\",\"href\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\",\"href\":\"http://example.com/\"}]}"

// assuming item is an valid object
NSString *itemTitleOrURL = (item.title == nil) ? SHKEncodeURL(item.URL) : SHKEncode(item.title);

dialog.attachment = [NSString stringWithFormat:SHK_ATTACHMENT_TEMPLATE_STR, itemTitleOrURL, SHKEncodeURL(item.URL)];

Following a readable code style will definitely save you time while solving similar syntax errors.

有深☉意 2025-01-05 15:34:54

三向条件句中缺少一个冒号;

原来的代码是这样的:

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@\",\"href\"
:\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL) 
SHKEncode(item.title),SHKEncodeURL(item.URL)];

改成这样

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@\",\"href\"
:\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL): 
SHKEncode(item.title),SHKEncodeURL(item.URL)];

错误在第三行末尾。

您应该在 XCode 编辑器中看到过这一点,在错误所在的位置下方有一个黄色的小字符。

You are missing an colon in the three-way conditional;

The original code looks like this:

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@\",\"href\"
:\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL) 
SHKEncode(item.title),SHKEncodeURL(item.URL)];

Change it to this

dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"%@\",\"href\"
:\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://example.com/example.png\"
,\"href\": \"http://example.com/\"}]} ",item.title == nil ? SHKEncodeURL(item.URL): 
SHKEncode(item.title),SHKEncodeURL(item.URL)];

The error is at the end of the third line.

You should have seen this in the XCode editor, with a little yellow charat under the position where the error is.

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