About the questions of NSDictionary

Joined
May 27, 2012
Messages
5
Reaction score
0
Points
1
I am using NSDictionay, but some questions really puzzled me.
Following is the related code

Code:
@interface AttributedStringDoc : NSObject {
	// ....... other properties

@private	
	// ..... other privates
	NSDictionary		*pageRangesInfo;
}
@end



@implementation AttributedStringDoc

// ....... other function's implementation

// this function is called by the init*** function
- (void)loadWithFileName {

	// ....... other codes

	pageRangesInfo = [[NSDictionary alloc] initWithContentsOfFile:filePath];

	// .........other codes
}


-(NSDictionary *)rangeInfoForPage:(NSInteger)pageNumber {
	return [pageRangesInfo objectForKey:[NSString stringWithFormat:@"%d", pageNumber]];
}
@end


The main content of file used to to be parsed by NSDictionary is formed like following:
Code:
<key>0</key>
<dict>
  <key>ASD_RANGE_END_INDEX</key>
  <integer>1256</integer>
<dict>
<key>1</key>
<dict>
  <key>ASD_RANGE_END_INDEX</key>
  <integer>5464</integer>
<dict>

The function rangeInfoForPage will be called several time. After called some times, the program will crash. But if I only return nil in function rangeInfoForPage, then every thing is OK. And I debugged the program also, I found function rangeInfoForPage can return correct value for some first steps.

So, I am wondering whether I am not using NSDictionary correctly?
Could any one help me, I am studying iOS programming by myself, and I'm really blocked by this!
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Your rangeInfoForPage method is returning a NSString, when you have implemented the method to return an NSDictionary.

Also in your dictionary file, the pageNumber is listed as a Integer, which is not a String.

So in short, your making some very basic errors, I think you need to learn the basics of Objective-C, I admire your enthusiasm, but you need to get back to some basics, and perhaps read a beginners book, like this one.

Amazon.com: Objective-C Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides) (9780321706287): Aaron Hillegass: Books

Regards Mark
 
OP
C
Joined
May 27, 2012
Messages
5
Reaction score
0
Points
1
Your rangeInfoForPage method is returning a NSString, when you have implemented the method to return an NSDictionary.

Also in your dictionary file, the pageNumber is listed as a Integer, which is not a String.

So in short, your making some very basic errors, I think you need to learn the basics of Objective-C, I admire your enthusiasm, but you need to get back to some basics, and perhaps read a beginners book, like this one.

Amazon.com: Objective-C Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides) (9780321706287): Aaron Hillegass: Books

Regards Mark

It worked, there is no problem for the listed codes. But thanks also.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top