Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Problems with NSDictionary

Problems with NSDictionary
Thread Tools
LordRPI
Fresh-Faced Recruit
Join Date: Aug 2001
Status: Offline
Reply With Quote
Jan 21, 2002, 03:55 PM
 
I have the December 2001 developer tools.

What I'm trying to do is create a dictionary entry, and literally I copied this function out of a book, but it really doesn't work

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
-(NSDictionary *)createRecord
{
��NSMutableDictionary *record = [[NSMutableDictionary alloc] init];

��[record setObject:<font color = orange>@"name<font color = red>"</font> forKey:<font color = orange>@"</font>First Name"</font>];
��[record setObject:<font color = orange>@"name<font color = red>"</font> forKey:<font color = orange>@"</font>Last Name"</font>];
��[record setObject:<font color = orange>@"name<font color = red>"</font> forKey:<font color = orange>@"</font>Email"</font>];
��[record setObject:<font color = orange>@"name<font color = red>"</font> forKey:<font color = orange>@"</font>Home Phone"</font>];

��[record autorelease];
��return record;
}
</font>[/code]

I get the follwing build errors in PB:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
SearchData.m:<font color = blue>14</font>: illegal expression, found `unknown'
SearchData.m:<font color = blue>16</font>: illegal expression, found `unknown'
SearchData.m:<font color = blue>17</font>: illegal expression, found `unknown'
SearchData.m:<font color = blue>18</font>: illegal expression, found `unknown'
SearchData.m:<font color = blue>19</font>: illegal expression, found `unknown'
SearchData.m:<font color = blue>21</font>: illegal expression, found `unknown'
SearchData.m:<font color = blue>22</font>: illegal expression, found `unknown'
cpp-precomp: warning: errors during smart preprocessing, retrying in basic mode
SearchData.m: In function `-[SearchData createRecord]':
SearchData.m:<font color = blue>16</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>16</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>16</font>: warning: `NSMutableDictionary' does not respond to `setObject:forKey:'
SearchData.m:<font color = blue>17</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>17</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>17</font>: warning: `NSMutableDictionary' does not respond to `setObject:forKey:'
SearchData.m:<font color = blue>18</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>18</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>18</font>: warning: `NSMutableDictionary' does not respond to `setObject:forKey:'
SearchData.m:<font color = blue>19</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>19</font>: Cannot find interface declaration for `NSConstantString'
SearchData.m:<font color = blue>19</font>: warning: `NSMutableDictionary' does not respond to `setObject:forKey:'
</font>[/code]

What's wrong?
     
TheBum
Mac Enthusiast
Join Date: Sep 2001
Location: McKinney, TX
Status: Offline
Reply With Quote
Jan 21, 2002, 07:18 PM
 
In your setObject:forKey: message, try something like this:

[record setObject:[NSString stringWithString:@"name"] forKey:@"First Name"];

Replace "First Name" with your other keys in the other messages. I suspect the problem is that you're trying to add a constant literal NSString object into the record and the compiler doesn't like it. I don't have my Mac handy, so I'm just speaking from experience.
     
lindberg
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jan 22, 2002, 02:32 PM
 
No, the code as posted is just fine. @"name" is a perfectly valid NSString.

However, at some point you need to declare all the classes you're making use of, meaning you have to import their headers. At the top of your .m file, under the #import "MyHeader.h", add a couple lines like:

#import &lt;Foundation/NSString.h&gt;
#import &lt;Foundation/NSDictionary.h&gt;

That should do the trick. The weird NSConstantString error messages happen when you use constant strings in the source code without ever importing the NSString.h header. They are poor error messages to be sure.

If you have lots of Foundation headers to include, then &lt;Foundation/Foundation.h&gt; will import all of them.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:07 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,