'NSDictionary'可能无法响应'-objectAtIndex:'

时间:2011-05-25 11:52:33

标签: iphone nsdictionary

代码中没有错误,但我的词典没有加载

CODE:

//
//  SectionsViewController.m
//  Sections
//
//  Created by Kunwar Handa on 25/05/11.
//  Copyright 2011 Fortune4  Technologies. All rights reserved.
//

#import "SectionsViewController.h"

@implementation SectionsViewController
@synthesize names;
@synthesize keys;

#pragma mark -
#pragma mark UIViewController Methods

-(void)viewDidLoad{


    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.names= dict;
    [dict release];

    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.keys= array;

}



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [names release];
    [keys release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return [keys count];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];

    static NSString *SectionTableIdentifier = @"SectionTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionTableIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionTableIdentifier] autorelease];
    }

                cell.text = [nameSection objectAtIndex:row];
                return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    NSString *key = [keys objectAtIndex:section];
    return key;

}
@end




**LOG:**



Build Sections of project Sections with configuration Debug

ProcessInfoPlistFile /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app/Info.plist Sections-Info.plist
cd /Users/kunwarhanda/Documents/Sections
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
builtin-infoPlistUtility Sections-Info.plist -genpkginfo /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app/PkgInfo -expandbuildsettings -format binary -platform iphonesimulator -o /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app/Info.plist

CompileXIB /Users/kunwarhanda/Documents/Sections/MainWindow.xib
cd /Users/kunwarhanda/Documents/Sections
setenv IBC_MINIMUM_COMPATIBILITY_VERSION 4.2
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/usr/bin/ibtool --errors --warnings --notices --output-format human-readable-text --compile /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app/MainWindow.nib /Users/kunwarhanda/Documents/Sections/MainWindow.xib --sdk /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk

CompileXIB /Users/kunwarhanda/Documents/Sections/SectionsViewController.xib
cd /Users/kunwarhanda/Documents/Sections
setenv IBC_MINIMUM_COMPATIBILITY_VERSION 4.2
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/usr/bin/ibtool --errors --warnings --notices --output-format human-readable-text --compile /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app/SectionsViewController.nib /Users/kunwarhanda/Documents/Sections/SectionsViewController.xib --sdk /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk

ProcessPCH /var/folders/6o/6oRlqcHSG3i+gabLJ+MCV++++TM/-Caches-/com.apple.Xcode.502/SharedPrecompiledHeaders/Sections_Prefix-gyjbjzxosthibsbszggxsbxqehxr/Sections_Prefix.pch.gch Sections_Prefix.pch normal i386 objective-c com.apple.compilers.gcc.4_2
cd /Users/kunwarhanda/Documents/Sections
setenv LANG en_US.US-ASCII
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -x objective-c-header -arch i386 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk -fexceptions -fvisibility=hidden -mmacosx-version-min=10.6 -gdwarf-2 -fobjc-abi-version=2 -fobjc-legacy-dispatch -D__IPHONE_OS_VERSION_MIN_REQUIRED=40200 -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-generated-files.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-own-target-headers.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-all-target-headers.hmap -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-project-headers.hmap -F/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator -I/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/include -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources/i386 -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources -c /Users/kunwarhanda/Documents/Sections/Sections_Prefix.pch -o /var/folders/6o/6oRlqcHSG3i+gabLJ+MCV++++TM/-Caches-/com.apple.Xcode.502/SharedPrecompiledHeaders/Sections_Prefix-gyjbjzxosthibsbszggxsbxqehxr/Sections_Prefix.pch.gch

CompileC build/Sections.build/Debug-iphonesimulator/Sections.build/Objects-normal/i386/main.o /Users/kunwarhanda/Documents/Sections/main.m normal i386 objective-c com.apple.compilers.gcc.4_2
cd /Users/kunwarhanda/Documents/Sections
setenv LANG en_US.US-ASCII
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -x objective-c -arch i386 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk -fexceptions -fvisibility=hidden -mmacosx-version-min=10.6 -gdwarf-2 -fobjc-abi-version=2 -fobjc-legacy-dispatch -D__IPHONE_OS_VERSION_MIN_REQUIRED=40200 -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-generated-files.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-own-target-headers.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-all-target-headers.hmap -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-project-headers.hmap -F/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator -I/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/include -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources/i386 -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources -include /var/folders/6o/6oRlqcHSG3i+gabLJ+MCV++++TM/-Caches-/com.apple.Xcode.502/SharedPrecompiledHeaders/Sections_Prefix-gyjbjzxosthibsbszggxsbxqehxr/Sections_Prefix.pch -c /Users/kunwarhanda/Documents/Sections/main.m -o /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Objects-normal/i386/main.o

CompileC build/Sections.build/Debug-iphonesimulator/Sections.build/Objects-normal/i386/SectionsAppDelegate.o /Users/kunwarhanda/Documents/Sections/Classes/SectionsAppDelegate.m normal i386 objective-c com.apple.compilers.gcc.4_2
cd /Users/kunwarhanda/Documents/Sections
setenv LANG en_US.US-ASCII
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -x objective-c -arch i386 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk -fexceptions -fvisibility=hidden -mmacosx-version-min=10.6 -gdwarf-2 -fobjc-abi-version=2 -fobjc-legacy-dispatch -D__IPHONE_OS_VERSION_MIN_REQUIRED=40200 -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-generated-files.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-own-target-headers.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-all-target-headers.hmap -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-project-headers.hmap -F/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator -I/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/include -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources/i386 -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources -include /var/folders/6o/6oRlqcHSG3i+gabLJ+MCV++++TM/-Caches-/com.apple.Xcode.502/SharedPrecompiledHeaders/Sections_Prefix-gyjbjzxosthibsbszggxsbxqehxr/Sections_Prefix.pch -c /Users/kunwarhanda/Documents/Sections/Classes/SectionsAppDelegate.m -o /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Objects-normal/i386/SectionsAppDelegate.o

CompileC build/Sections.build/Debug-iphonesimulator/Sections.build/Objects-normal/i386/SectionsViewController.o Classes/SectionsViewController.m normal i386 objective-c com.apple.compilers.gcc.4_2
cd /Users/kunwarhanda/Documents/Sections
setenv LANG en_US.US-ASCII
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -x objective-c -arch i386 -fmessage-length=0 -pipe -std=c99 -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk -fexceptions -fvisibility=hidden -mmacosx-version-min=10.6 -gdwarf-2 -fobjc-abi-version=2 -fobjc-legacy-dispatch -D__IPHONE_OS_VERSION_MIN_REQUIRED=40200 -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-generated-files.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-own-target-headers.hmap -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-all-target-headers.hmap -iquote /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Sections-project-headers.hmap -F/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator -I/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/include -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources/i386 -I/Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/DerivedSources -include /var/folders/6o/6oRlqcHSG3i+gabLJ+MCV++++TM/-Caches-/com.apple.Xcode.502/SharedPrecompiledHeaders/Sections_Prefix-gyjbjzxosthibsbszggxsbxqehxr/Sections_Prefix.pch -c /Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m -o /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Objects-normal/i386/SectionsViewController.o

/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m: In function '-[SectionsViewController viewDidLoad]':
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:27: warning: incompatible Objective-C types 'struct NSArray *', expected 'struct NSDictionary *' when passing argument 1 of 'setKeys:' from distinct Objective-C type
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m: In function '-[SectionsViewController tableView:numberOfRowsInSection:]':
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:99: warning: 'NSDictionary' may not respond to '-objectAtIndex:'
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:99: warning: (Messages without a matching method signature
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:99: warning: will be assumed to return 'id' and accept
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:99: warning: '...' as arguments.)
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m: In function '-[SectionsViewController tableView:cellForRowAtIndexPath:]':
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:110: warning: 'NSDictionary' may not respond to '-objectAtIndex:'
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:120: warning: 'setText:' is deprecated (declared at /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/System/Library/Frameworks/UIKit.framework/Headers/UITableViewCell.h:211)
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m: In function '-[SectionsViewController tableView:titleForHeaderInSection:]':
/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:127: warning: 'NSDictionary' may not respond to '-objectAtIndex:'

Ld build/Debug-iphonesimulator/Sections.app/Sections normal i386
cd /Users/kunwarhanda/Documents/Sections
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk -L/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator -F/Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator -filelist /Users/kunwarhanda/Documents/Sections/build/Sections.build/Debug-iphonesimulator/Sections.build/Objects-normal/i386/Sections.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework Foundation -framework UIKit -framework CoreGraphics -o /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app/Sections

GenerateDSYMFile build/Debug-iphonesimulator/Sections.app.dSYM build/Debug-iphonesimulator/Sections.app/Sections
cd /Users/kunwarhanda/Documents/Sections
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/usr/bin/dsymutil /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app/Sections -o /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app.dSYM

Touch build/Debug-iphonesimulator/Sections.app
cd /Users/kunwarhanda/Documents/Sections
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/usr/bin/touch -c /Users/kunwarhanda/Documents/Sections/build/Debug-iphonesimulator/Sections.app

5 个答案:

答案 0 :(得分:2)

NSDictionary,根据文档,不应回复objectAtIndex:,这是NSArray的方法。 NSDictionary的等效值为objectForKey:

答案 1 :(得分:1)

鉴于帖子中几乎完全没有格式化,很难看出问题所在。从你问题的标题我猜你应该在调用'objectForKey'时在NSDictionary上调用'objectAtIndex'。

答案 2 :(得分:0)

这是真的 - 'NSDictionary'不响应'-objectAtIndex:'

编译器指出问题的源代码行,例如:     SectionsViewController.m:99

和xcode也可能会为你突出显示它。

答案 3 :(得分:0)

objectAtIndex:是一种数组方法,不能用于字典。您正尝试使用objectAtIndex:访问班级中某个字典中的对象。检查您是否正在访问NSDictionary

答案 4 :(得分:0)

如果出现错误,您希望将keys声明为NSDictionary NSArray。因此,警告您在第27行分配了错误类型的对象,然后在第99行进行了可能不兼容的调用。

您应该检查声明密钥的方式,这些密钥将在关联的头文件中,而不是在您给出的代码中。解决第一个警告:

  

/Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:   在函数' - [SectionsViewController   viewDidLoad中]“:   /Users/kunwarhanda/Documents/Sections/Classes/SectionsViewController.m:27:   警告:不兼容的Objective-C   类型'struct NSArray *',预期   传递时'struct NSDictionary *'   'setKeys:'的参数1来自distinct   Objective-C类型

将解决所有问题。