|
|
Problems with NSDictionary
|
|
|
|
Fresh-Faced Recruit
Join Date: Aug 2001
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Sep 2001
Location: McKinney, TX
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
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 <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
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 <Foundation/Foundation.h> will import all of them.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|