在 Obj C 中生成字符串列表
Objective C 似乎是在不断地突破重围,让看似简单的任务变得异常困难。
我只需要创建一系列字符串,image1.jpg
、image2.jpg
等,
即在循环中 var imgString:String='image'+i+'.jpg;
我认为最佳实践是使用 NSMutableString
和 appendString
方法?
我做错了什么?
NSMutableString *imgString;
for(int i=1;i<=NUMIMAGES;i++){
imgString.appendString(@"image"+i+@".jpg");
}
我收到以下错误
错误:请求非结构中的成员“appendString” 或联盟
It seems that Objective C jumps thru hoops to make seemingly simple tasks extremely difficult.
I simply need to create a sequence of strings, image1.jpg
, image2.jpg
, etc etc
ie in a loopvar imgString:String='image'+i+'.jpg;
I assume a best practice is to use a NSMutableString
with appendString
method?
What am I doing wrong??
NSMutableString *imgString;
for(int i=1;i<=NUMIMAGES;i++){
imgString.appendString(@"image"+i+@".jpg");
}
I get the following error
error: request for member 'appendString' in something not a structure
or union
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我无法想象如何在您自己的问题的“答案”中产生字符串
image5image4image3image2image1.jpg
instring3
可以被解释为有用或解决方案(我将您的代码粘贴到一个 Xcode Foundation 工具项目,NUMIMAGES 设置为 5)。Objective C 似乎跳过了重重困难,使看似简单的任务变得极其困难。
阅读Objective-C 指南 和 字符串编程指南,然后得出结论。批评你不理解的东西通常会适得其反(不过,我们都这样做过——在沮丧时很容易做到)。
我只需要创建一系列字符串、image1.jpg、image2.jpg 等
朴素的解决方案:
输出:
请注意,朴素的解决方案没有任何特别的错误。它只是更脆弱,效率更低,并且代码多于所需。
一种可能的更好解决方案
将所有图像作为资源添加到您的应用程序项目中。
使用 NSBundle API 一次性获取所有图像的 URL。
即:
这不仅可以从代码中删除图像名称的硬编码,还允许系统优化目录枚举和路径生成。如果您只想加载图像的子集 - 假设您正在编写一个游戏并且有一系列与怪物相关的图像 - 然后将所有相关图像放在一个目录中,将该目录添加到您的资源中,然后传递该目录目录的名称作为上述方法的第二个参数。
所有这些 - 以及更多 - 都记录在 资源编程指南。
I cannot possibly imagine how yielding the string
image5image4image3image2image1.jpg
instring3
in your "answer" to your own question could be construed as useful or a solution (I pasted your code into an Xcode Foundation tool project w/NUMIMAGES set to 5).It seems that Objective C jumps thru hoops to make seemingly simple tasks extremely difficult.
Read the Objective-C Guide and the String Programming Guide, then make draw your conclusions. Criticizing something you don't understand is generally counter-productive (we've all done it, though -- easy to do when frustrated).
I simply need to create a sequence of strings, image1.jpg, image2.jpg, etc etc
Naive solution:
Outputs:
Note that there isn't anything particularly wrong with the naive solution. It is just more fragile, less efficient, and more code than is necessary.
One Possible Better Solution
Add all images into your application project as resources.
Use the NSBundle API to grab URLs to all images at once.
I.e.:
Not only does this remove hardcoding of image names from your code, it also allows the system to optimize directory enumeration and path generation. If you want to load only a subset of images -- say you are writing a game and have a series of images related to monsters -- then put all the related images in a single directory, add the directory to your Resources, then pass that directory's name as the second argument to the above method.
All of this -- and much more -- is documented in the resource programming guide.
首先,您调用方法的语法已关闭 - 我建议阅读Apple Objective-C 简介。值得注意的是,要调用方法,请使用
[someObject someMethod]
语法。下一个问题是您永远不会创建要附加的字符串,您可以使用例如
[NSMutableString string]
来实现。继续,要将可变字符串与某些格式化字符串连接起来,请使用
-appendFormat:
,例如:First, your syntax for invoking methods is off - i recommend reading through Apples Introduction to Objective-C. Notably, to invoke a method you use the
[someObject someMethod]
syntax.The next problem is that you never create the string you are trying to append to, you can use e.g.
[NSMutableString string]
for that.Continuing, to concatenate a mutable string with some formatted string use
-appendFormat:
, e.g.:我不太确定你想做什么,但这可能是一个灵感:
至于你的错误,你使用的点语法没有任何意义。另外,您不可以使用
+
在 Objective-C 中连接字符串。正确的appendString
语法如下所示:正如您在第一行中看到的,在使用对象之前初始化对象是一个好主意:) 要连接字符串,您可以使用
appendString<
NSMutableString
的 /code> 方法,或者您可以创建一个新的不可变字符串:如果您有更多字符串要连接,您可以使用
stringWithFormat
初始值设定项:如果您仍然有这种感觉Objective-C 让某些事情变得极其困难,你可能通过实际学习它获得一些优势;)
I’m not really sure what you’re trying to do, but this might be an inspiration:
As for your error, you’re using the dot syntax where it doesn’t make any sense. Also, you don’t use the
+
to concatenate strings in Objective-C. The correctappendString
syntax looks like this:As you can see on the first line, it is a good idea to initialize objects before you use them :) To concatenate strings you can use the
appendString
method ofNSMutableString
or you can create a new immutable string:If you have more strings to concatenate, you can use the
stringWithFormat
initializer:If you still have the feeling that Objective-C makes certain things extremely difficult you might gain some advantage by actually learning it ;)
执行您想要的操作的最简单的代码将是这样的:
就是这样。正如您所看到的,
appendFormat:
确实让事情变得很多变得更容易!但我不明白你为什么要这样做。你会得到类似这样的信息:你到底想实现什么目标?
戴夫
The simplest code which does what you want would be something like this:
That's it. As you can see,
appendFormat:
really makes things a lot easier! But I can't figure out why you would want to do this. You'll get something like:What are you actually trying to achieve?
Dave
感谢所有的答案!
有时感觉一个人需要成为一名受虐狂才能精通 Objective C!。
将尝试上述方法,但最终使用
Appreciate all the answers!
Sometimes it feels that one needs to be a masochist to be proficient in Objective C!.
Will try the above but ended up using