[__NSCFString objectAtIndex:]: unrecognized selector sent to instance Error - objective-c

I tried loading the contents of the bucket into a NSTableView using Arraycontroller. I ended uo with this error:
-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x1006da970
Here is the code I used to check.
- (IBAction)checkCloud:(id)sender {
AmazonS3Client *s3 = [AmazonClientManager s3];
S3ListObjectsRequest* listObjectsRequest = [[S3ListObjectsRequest alloc] initWithName:#"hello-testing"];
NSMutableArray *fileListCloud = [[NSMutableArray alloc] init];
NSMutableArray *fileModifiedList = [[NSMutableArray alloc] init];
#try {
S3ListObjectsResponse* response = [s3 listObjects:listObjectsRequest];
NSMutableArray* objectSummaries = response.listObjectsResult.objectSummaries;
for ( S3ObjectSummary* objSummary in objectSummaries ) {
[fileListCloud addObject:[objSummary key]];
[fileModifiedList addObject:[objSummary lastModified]];
}
}
#catch (NSException *exception) {
NSLog(#"Cannot list S3 %#",exception);
}
[self loadFileListTable:fileListCloud andFileModificationDate:fileModifiedList];
}
-(void)loadFileListTable:(NSMutableArray *)fileListArray andFileModificationDate:(NSMutableArray *)modifiedArray{
NSRange range = NSMakeRange(0, [[_fileListAC arrangedObjects] count]);
[_fileListAC removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
for(int i = 0 ; i<[fileListArray count];i++){
[_fileListAC addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[[fileListArray objectAtIndex:i] objectAtIndex:0],#"Files",[[modifiedArray objectAtIndex:i] objectAtIndex:1],#"Date Modified", nil]];
}
}
The array is loaded with with file names but when i add it to the array controller it throws the above mentioned error. Any help would be really great.

Here
[[fileListArray objectAtIndex:i] objectAtIndex:0]
[[modifiedArray objectAtIndex:i] objectAtIndex:1]
[fileListArray objectAtIndex:i] and [modifiedArray objectAtIndex:i] are not NSArrays (I think they are both NSStrings).
You just need to remove the erroneous objectAtIndex:0 and objectAtIndex:1 messages.

Change here
[_fileListAC addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[fileListArray objectAtIndex:i],#"Files",[modifiedArray objectAtIndex:i], #"Date Modified", nil]];

Related

App keeps crashing creating a PygLatin Game - Objective C

I am new to objective C programming.
I created a PygLatin application but it keeps crashing it. Do tell me if you have spotted any problems with my code.
Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
First throw call stack:
would appreciate all the help I can get thanks! :)
#implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.userInputTextField.delegate = self;
self.userInputTextField.text = #"";
}
-(void)print: (NSString*)printWords {
self.resultLabel.text = #"%#", printWords;
}
-(void)charsplitter: (NSArray*)charArraysplitter {
//word selection loop
int indexCount = 0;
for(indexCount = 0; indexCount < charArraysplitter.count; indexCount++)
{
self.wordsToBeSplit = charArraysplitter[indexCount];
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[self.wordsToBeSplit length]];
[self.wordsToBeSplit enumerateSubstringsInRange:NSMakeRange(0, self.wordsToBeSplit.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[characters addObject:substring];
}];
[self vowelsComparator:characters];
}
}
-(void)vowelsComparator: (NSArray*)comparator {
self.myVowels = [NSArray arrayWithObjects: #"a", #"e", #"i", #"o",#"u", nil];
int charIndex;
self.subsequentCharCount = 0;
for(charIndex = 0; charIndex < comparator.count; charIndex++)
//loops to find if first character is a vowel
if([self.myVowels containsObject:comparator[0]]){
[self print: self.userInputTextField]; NSLog(#"working fine:");
}else{
//loops to find other vowels
while (self.subsequentCharCount < comparator.count){
self.subsequentCharCount++;
if ([self.myVowels containsObject:comparator[self.subsequentCharCount]]){
//moving the consonants behing the vowels
NSLog(#"working fine:");
NSString *combinedWords = [[self.wordsToBeSplit substringFromIndex:self.subsequentCharCount]stringByAppendingString:[self.wordsToBeSplit substringToIndex:self.subsequentCharCount]];
NSString *completedWord = [combinedWords stringByAppendingString:#"ay"];
[self print: completedWord];
}
};
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];//resigns the keyboard every time return button is pressed
return YES;
}
- (IBAction)pygButton:(id)sender
{
self.inputText = [self.userInputTextField.text lowercaseString];//user input is lowercase
NSArray *wordsArray = [self.inputText componentsSeparatedByString: #" "]; //separate words into arrays.
[self charsplitter: wordsArray];
}
#end
put
self.subsequentCharCount++;
at the end of while loop means below
[self print: completedWord];

NSMutableDictionary using objectAtIndex

I am new to Xcode and I have created a NSMutableDictionary *dIc with following data:
Keys : ID , Name
Data :
ID = 1, Name = John
ID = 2, Name = Smith
I want to access dIc with specific row. I want this result for example :
Name = Smith.
I tried :
-(void)initiateArrays
{
dIc =[[NSMutableDictionary alloc] init];
}
- (void)viewDidLoad
{
[self initiateArrays];
[self callingWebSrvc];
[super viewDidLoad];
}
-(void)callingWebSrvc
{
NSInteger i=0;
do {
self.selectedRowId = [[mainMenuID objectAtIndex:i]integerValue];
[self getItemListFromWebSrvc];
i++;
} while (i<= mainMenuData.count - 1);
}
-(void) getItemListFromWebSrvc
{
//............ Request ....................................>>>
.
.
.
//............ Response ....................................>>>
NSInteger i = 0;
do {
elm = [xmlDataTable objectAtIndex:i];
//Set Object into Arrays
[dIc setObject:[NSNumber numberWithInt:selectedRowId] forKey:#"Id"];
[dIc setObject:[elm childNamed:#"Name"].value forKey:#"Name"];
NSArray *keys = [rowArray allKeys];
// values in foreach loop
for (NSString *key in keys) {
NSLog(#"%# is %#",key, [dIc objectForKey:key ]);
}
i=i+1;
} while (i <= xmlDataTable.count-1);
}
-(void)showResult
{
NSString *anObject = [[dIc objectForKey:#"Name" ] objectAtIndex:1];
NSLog(#"The Value is = %#",anObject );
}
but it's not working.
when i call [self showResult]; i got the following error:
-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x8b9a7f0
2014-02-26 00:53:57.718 iWish[3311:70b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x8b9a7f0'
The problem is this line:
NSString *anObject = [[dIc objectForKey:#"Name" ] objectAtIndex:1];
The first object in this story, [dIc objectForKey:#"Name" ], is an NSString. You are then saying objectAtIndex:1 to an NSString - and that is a no-no.

uisearchbar in grouped section uitable

I've pieced together several tutorials to create a grouped table with sections and I'm now trying to get a uisearchbar to work. the problem I'm having is how to search within the grouped sections.
I've read the similar questions this post suggested but can't
This is the code to create the grouped sections
#import "Job.h" // A model for the data
#import "Address.h" // Another model for the data
- (void)viewDidLoad
{
[super viewDidLoad];
self.theTable.delegate = self;
self.theTable.dataSource =self;
_searchBar.delegate = (id)self;
FMDBDataAccess *db = [[FMDBDataAccess alloc] init];
jobs = [[NSMutableArray alloc] init];
jobs = [db getJobs:1];
_sections = [[NSMutableDictionary alloc] init];
NSMutableArray *jobsTempArray = [db getJobsAsDictionary:1];
BOOL found;
// Loop through the books and create our keys
for (NSDictionary *book in jobsTempArray)
{
NSString *cLong = [book objectForKey:#"addrAddress"];
NSString *c = [cLong substringToIndex:1];
found = NO;
for (NSString *str in [_sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[_sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
// Loop again and sort the books into their respective keys
for (NSDictionary *book in jobsTempArray)
{
[[_sections objectForKey:[[book objectForKey:#"addrAddress"] substringToIndex:1]] addObject:book];
}
// Sort each section array
for (NSString *key in [_sections allKeys])
{
[[_sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"addrAddress" ascending:YES]]];
}
}
And this is the code that searches
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
_isFiltered = FALSE;
}
else
{
_isFiltered = true;
_filteredjobs = [[NSMutableArray alloc] init];
//for (Job* book in jobs)
//for (Job* book in [_sections allKeys])
//for (NSString *food in [_sections allKeys])
for (NSDictionary* book in [_sections allKeys])
{
NSString *addrStr = [book objectForKey:#"addrAddress"];
NSString *postStr = [book objectForKey:#"addrPostcode"];
//NSRange nameRange = [book.jobAddress rangeOfString:text options:NSCaseInsensitiveSearch];
NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch];
//NSRange descriptionRange = [book.jobPostcode rangeOfString:text options:NSCaseInsensitiveSearch];
NSRange descriptionRange = [postStr rangeOfString:text options:NSCaseInsensitiveSearch];
if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
{
[_filteredjobs addObject:book];
}
}
}
[self.theTable reloadData];
}
I've got as far as realising I need to change for (Job* food in jobs) to for (NSDictionary* book in [_sections allKeys]) but I'm stuck how to search within [_sections allKeys]
Specifically this line
NSRange nameRange = [addrStr rangeOfString:text options:NSCaseInsensitiveSearch];
which crashes with
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x692e200
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]:
unrecognized selector sent to instance 0x692e200':
Any ideas? PS Treat me as a noob, I'll need some code as well as explanation - I'm still learning obj-c
Check the Link.It shows the UISearchBar With Grouped Section Tableview.Its a simple Tutorial..Hope its useful for you
I found the answer in
UISearchBar - search a NSDictionary of Arrays of Objects and reading up on allkeys.
Basically loop through the grouped NSDictionary and extract the NSArrays, then loop through again searching...
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
_isFiltered = FALSE;
}
else
{
_isFiltered = true;
_filteredjobs = [[NSMutableArray alloc] init];
NSString *currentLetter = [[NSString alloc] init];
for (int i=0; i<[_sections count]; i++)
{
currentLetter = [[_sections allKeys] objectAtIndex:i];
NSArray *jobsForKey = [ [NSArray alloc] initWithArray:[_sections objectForKey:[[_sections allKeys] objectAtIndex:i]] ];
for (int j=0; j<[jobsForKey count]; j++)
{
NSDictionary *book = [jobsForKey objectAtIndex:j];
NSRange titleResultsRange = [[book objectForKey:#"addrAddress"] rangeOfString:text options:NSCaseInsensitiveSearch];
if(titleResultsRange.location != NSNotFound)
{
[_filteredjobs addObject:book];
}
}
}
}
[self.theTable reloadData];
}

getting NCFString error while searching the tableview using searchbar

i ve got a table view parsed from json string.everthing works good .if i m to type a character in my search bar i m getting this error '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1d52ac50'below is the code.were the below variable name data is NSMutableArray.could u guys help me out.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controllershouldReloadTableForSearchString:(NSString *)searchString
{
[searchData removeAllObjects];
NSArray *group;
for(group in nameData)
{
NSLog(#"wat am i doing here:%#",group);
NSMutableArray *newGroup = [[NSMutableArray alloc] init];
NSString *element;
for(element in group)
{
NSRange range = [element rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (range.length > 0) {
[newGroup addObject:element];
}
}
if ([newGroup count] > 0) {
[searchData addObject:newGroup];
}
}
return YES;
}
Please check out this code
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSMutableArray *array=[[NSMutableArray alloc]init];
NSDictionary *naming= [self.friendsDictionary objectForKey:#"data"];
NSLog(#"ggg %#",[self.friendsDictionary objectForKey:#"data"]);
for(int i =0 ; i< [[self.friendsDictionary objectForKey:#"data"] count]; i++){
[array addObject:[[[self.friendsDictionary objectForKey:#"data"] objectAtIndex:i] objectForKey:#"name"]];
}
NSLog(#"Array %#", array);
if(self.searchDisplayController.searchBar.text.length>0)
{
NSString *strSearchText = self.searchDisplayController.searchBar.text;
NSMutableArray *group = [[NSMutableArray alloc]init];
for (int i=0; i<array.count; i++) {
NSString *strData = [array objectAtIndex:i];
NSLog(#"string Data:%#",strData);
NSRange rng = [strData rangeOfString:strSearchText options:NSCaseInsensitiveSearch];
if(rng.location != NSNotFound)
{
if(rng.location== 0)//that is we are checking only the start of the names.
{
[group addObject:strData];
}
}
}
if ([group count] > 0)
{
searchData = [[NSMutableArray alloc] init];
[searchData addObjectsFromArray:group];
NSLog(#"Sear %#", searchData);
}
}
return YES;
}

Initialization of an NSDictionary in Objective C (for iOS)

I am relatively new to Objective-C and now I have a problem in my iPhone app that I don't fully understand.
I try to use a NSMutableDictionary, this does not seem to work as i expect for some reason. When I run the debugger and do po numberToCallerMap to see the dictionary, I get an exception. I have read the documentation for NSMutableDictionary on how to initialize it, but I can not see what I am doing wrong. Help and advice are appreciated. The variable causing me problem is numberToCallerMap, here is the relevant function:
- (void)setData:(NSString*)value{
[list release];
list = [[NSMutableArray alloc] init];
SBJSON *json = [[[SBJSON alloc] init] autorelease];
NSMutableDictionary* numberToCallerMap;
CallerInfo* caller;
NSDictionary* callerInfo;
#try {
NSArray *array = (NSArray*)[json objectWithString:value];
// reading all the items in the array one by one
numberToCallerMap = [NSMutableDictionary dictionary];
for (id *item in array) {
// if the item is NSDictionary (in this case ... different json file will probably have a different class)
NSDictionary *dict2 = (NSDictionary *) item;
CallInfo *data = [CallInfo alloc];
[data initFromDictionary:dict2];
callerInfo = (NSDictionary*)[dict2 valueForKey:#"caller"] ;
//Here, we want the phonenumber to be part of the CallerInfo object instead.
// It is sent from the server as part of the Call-object
NSString* number = (NSString*)[dict2 valueForKey:#"phoneNumber"];
[callerInfo setValue:number forKey:#"phoneNumber"];
caller = (CallerInfo*)[numberToCallerMap valueForKey:number];
if(caller == nil || [caller isKindOfClass:[NSNull class]]){
caller = [CallerInfo alloc];
[caller initFromDictionary:callerInfo];
[numberToCallerMap setValue:caller forKey:number];
[list insertObject:caller atIndex:0];
}
[caller addRecentCall:data];
}
}
#catch (NSException * e) {
[list release];
list = [[NSMutableArray alloc] init];
}
#finally {
[numberToCallerMap release];
}
}
This is probably not the only problem, but you are not alloc-ing your numberToCallerMap dictionary, you are getting it from a convenience class method -- [NSMutableDictionary dictionary] -- that returns it autoreleased. So you should not call release on it yourself.