将 NSDictionary 中的 objectAtKey 字符串设置为单独类中的 NSString
我从 URL(JSON) 获取了信息;我可以轻松地使用字典 [aCategory objectAtKey:@"names"]
中的文本填充 tableView 作为单元格标签。现在,根据单元格名称(类别),我想显示另一个表格,该表格将询问最后一个 URL,以便根据该类别获取其余文本。
因此,我想要将 [aCategory objectAtKey:@"ID"]
中的 ID 保存到字符串中,并将其放入下一个 viewController 中的 URL 中。我目前正在尝试使用自动生成的 set 方法来生成它,以填充目标视图控制器中的 NSString *ID ;当我打电话时 [newView setID: [aCategory objectAtKey:@"ID"]
。
我的新视图控制器的 NSString ID 说它不为空,但是当我尝试检查它是否确实是“1”或“2”时,我得到类似 /p2002
之类的信息。但是,在原始类中,如果我说 cell.detailLabelText.text = [aCategory objectAtKey:@"ID"];
,标签会正确显示“1”“2”“3”..” 14" 等...
那么,我如何从该键获取该 ID 到我的其他视图控制器类中?
我知道它是一个有效的 NSCFString,因为我使用 isClass
和单元格的 detailLabelText
对其进行了测试。
I have got information from a URL(JSON); I easily populate my tableView with the text from my dictionary [aCategory objectAtKey:@"names"]
for the cell labels. Now, based on the cell name (category), I want to display another table that will ask for the last URL in order to grab the rest of the text, based on that category.
So, i want an ID which is in [aCategory objectAtKey:@"ID"]
to save to a string and put in the URL in the next viewController. I am currently trying to generate it using the auto-generated set method to populate the NSString *ID in the target viewcontroller; when i call[newView setID: [aCategory objectAtKey:@"ID"]
.
My new view controller's NSString ID says it is not empty, but when i try to check to see if it is indeed "1" or "2" i get something like /p2002
or something. However, in the original class, if i say cell.detailLabelText.text = [aCategory objectAtKey:@"ID"];
, the labels correctly show "1" "2" "3".."14" etc....
SO, how can i get that ID from that key into my other viewcontroller class?
I Know it is a valid NSCFString cause i tested it both with isClass
AND with the cell's detailLabelText
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要更多细节来确定这一点,但是有一些注释...
NSString 就是所谓的类簇,这基本上意味着它实际上是底层框架在它们之间切换的几个不同类的集合...这意味着如果您检查它,
您可能不会得到您期望的结果。检查 NSObject 中 isKindOfClass: 的文档。
不过,我不确定是否有什么需要担心的。您看到类似“\p2002”(第一个字母是 au 吗?)的原因可能只是字符串的当前底层表示形式。有时,当设备将字符串内容保存在内存中时,它看起来并不完全像“1”或“2”。这并不意味着存在问题:它只是意味着,在更深的层次上,字符串在内存中的保存方式是不同的形式。这就是为什么你的标签可能会说“2”,但当你在内存中检查它时,变量看起来不同。
(我猜测,由于您正在处理 JSON,因此该字符串以一种称为 UTF-8 的形式进行编码。)重点是,根本不会有任何问题。
真正的问题是,您的新视图控制器是否正确加载?也许在新视图控制器的 viewDidLoad: 方法中,如果运行以下行:
这会将 stringID 的值打印到控制台。如果这个数字与前一个视图控制器中表格单元格标签的数字相同,则所有内容都应该已正确传递。
I need more detail to be sure about this, but a couple of notes...
NSString is what is called a class cluster, which basically means it is actually a collection of several class different classes that the underlying framework switches between...this means if you are checking it with
you might not be getting the results you are expecting. Check the documentation for isKindOfClass: in NSObject.
However, I am not sure if there is anything to worry about. The reason you are seeing something like '\p2002' (Could the first letter be a u?) could just be the current underlying representation of the string. Sometimes, when the device holds the content of the string in memory, it does not look exactly like "1" or "2". It doesn't mean that there is a problem: it just means that, a deeper level, the way the string is being held in memory is in different form. That is why your label might say "2" but the variable, when you check it in memory, looks different.
(I am guessing that, since you are handling JSON, the string is encoded in a form called UTF-8.) The point is, nothing may wrong at all.
The real question is, is your new view controller loading correctly or not? Maybe in the viewDidLoad: method of your new view controller, if you run something this line:
This will print the value of stringID to the console. If this number is the same as the number of the table cell's label in the previous view controller, everything should have been passed correctly.