No visible #interface for 'NSObject' declares the selector 'objectAtIndex:' - ios

I'm trying to iterate an array, however the code fails with the following error message:
No visible #interface for NSObject declares the selector objectAtIndex:
Here is how my code looks like:
_elements = [NSMutableArray array];
[_elements addObject:_arrins1 ];
[_elements addObject:_arrins2 ];
[_elements addObject:_arrins3 ];
_arrins1 = [NSMutableArray array];
[_arrins1 addObject:ins1.title = #"test1"];
[_arrins1 addObject:ins1.size = #90];
[_arrins1 addObject:ins1.text = #"example"];
[_arrins1 addObject:ins1.ids = #0];
_arrins2 = [NSMutableArray array];
[_arrins2 addObject:ins2.title = #"test2"];
[_arrins2 addObject:ins2.size = #40];
[_arrins2 addObject:ins2.text = #"example"];
[_arrins2 addObject:ins2.ids = #1];
_arrins3 = [NSMutableArray array];
[_arrins3 addObject:ins3.title = #"test3"];
[_arrins3 addObject:ins3.size = #20];
[_arrins3 addObject:ins3.text = #"example"];
[_arrins3 addObject:ins3.ids = #2];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 0;
for (NSObject *j in _elements) {
// Error: No visible #interface for 'NSObject' declares the selector 'objectAtIndex:'
height = [j objectAtIndex:1];
}
return height;
}
I also tried to change the type of elements in the loop, but it didn't help:
for (id j in _elements) {
// Error: Assigning to 'CGFloat' (aka 'double') from incompatible type 'id'
height = [j objectAtIndex:1];
}

Related

Error: [__NSArrayM addObject:], NIL object

I receive the following error: [__NSArrayM addObject:], NIL object. but not crash ,I want to know why display this remind.
here is my code
NSMutableArray *picturesCounts = [NSMutableArray array];
NSMutableArray *bigUrlArray = [NSMutableArray array];
NSMutableArray *pitcturesArray = [NSMutableArray array];
NSInteger i = 1;
for (TNHotelPictures *picture in pictures) {
NSString *pictureCounts = [NSString stringWithFormat:#"%ld",(long)picture.images.count];
[picturesCounts addObject:pictureCounts];
[pitcturesArray addObject:picture.images];
for (TNHotelDetailPhotoImages *photoImage in picture.images) {
[bigUrlArray addObject:photoImage.bigUrl];
}
i++;
}
NSInteger imageInAllIndex = imageIndex;
NSMutableArray *imagesCount = [NSMutableArray array];
for (NSInteger m = 0; m<i; m++) {
imagesCount = pitcturesArray[(typeIndex-(m+1))] ;
imageInAllIndex += imagesCount.count;
}
NSMutableArray *array = [NSMutableArray array];
for (NSString *url in bigUrlArray)
{
if (![url isKindOfClass:[NSString class]])
{
return;
}
TNHotelPhoto *photo = [[TNHotelPhoto alloc] initWithImageURL:[NSURL URLWithString:url]];
[array addObject:photo];
}
TNHotelPhotoSource *source = [[TNHotelPhotoSource alloc] initWithPhotos:array];
TNHotelDetailPhotoBrowserViewController *browserVC = [[TNHotelDetailPhotoBrowserViewController alloc] initWithPhotoSource:source WithImageIndex:imageInAllIndex>=0?imageInAllIndex:0 andImageArray:pictures andTypeIndex:typeIndex];
[UIManager showViewController:browserVC];
}
It means that the object has been initialized and holding a reference in memory. Its not a NIL.
See your code:
for (TNHotelPictures *picture in pictures) {
NSString *pictureCounts = [NSString stringWithFormat:#"%ld",(long)picture.images.count];
[picturesCounts addObject:pictureCounts];
[pitcturesArray addObject:picture.images];
for (TNHotelDetailPhotoImages *photoImage in picture.images) {
[bigUrlArray addObject:photoImage.bigUrl];
}
i++;
}
I think you should check the picture whether its properties pictureCounts,images,bigUrl are not nil.

How to add one NSArray data to another NSArray fields?

I tought question title is little bit mismatch , can you please see the following explanation...
Currently i am having 2 NSArray's of data:
NSArray *arr1 = [[NSArray alloc]initWithObjects:#"aa",#"bb",#"1",#"cc", nil];
NSArray *arr2 = #[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
In 1st Array i got data from server
I need to load those parameters in specific UILabel's in 2nd Array
I am looking O/P is::
self.lbl1.text = #"aa";
self.lbl2.text = #"bb";
self.lbl3.text = #"1";
self.lbl4.text = #"cc";
Is there any possiblity, can you please help me out..
in ViewController.h
#property (strong, nonatomic)IBOutlet UILabel* lbl1;
#property (strong, nonatomic)IBOutlet UILabel* lbl2;
#property (strong, nonatomic)IBOutlet UILabel* lbl3;
#property (strong, nonatomic)IBOutlet UILabel* lbl4;
in ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arr1 = [[NSArray alloc]initWithObjects:#"aa",#"bb",#"1",#"cc", nil];
NSArray *arr2 = #[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
for(int i=0;(i<[arr1 count])&&(i<[arr2 count]);i++)
{
UILabel *label = (UILabel*)[arr2 objectAtIndex:i];
label.text = (NSString*)[arr1 objectAtIndex:i];
}
NSLog(#"%#,\n %#,\n %#, \n%#",self.lbl1,self.lbl2,self.lbl3,self.lbl4);
}
CRASH like:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
Store the labels in the arrays, not their text property.
NSArray *arr1 = [[NSArray alloc]initWithObjects:#"aa",#"bb",#"1",#"cc", nil];
NSArray *arr2 = #[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
for(int i=0;(i<[arr1 count])&&(i<[arr2 count]);i++)
{
UILabel *label = (UILabel*)[arr2 objectAtIndex:i];
label.text = (NSString*)[arr1 objectAtIndex:i];
}
Checking for count of both like : (i<[arr1 count])&&(i<[arr2 count]) ensures that the app does not crash if somehow the arrays become of different count.
Short answer: It is Not possible the way you want to do it.
This is because self.lbl1.text is a NSString and not attached to self.lbl1 anymore (text is a copy property of UILabel). If you change this NSString, you
would not change the value of self.lbl1.text
A better approach may be to save only the label, not its value:
NSArray *arr2 = #[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
In this case, you could loop through your array:
for (NSInteger i=0; i < arr2.count && i < arr1.count; i++) {
((UILabel*)[arr2 objectAtIndex:i]).text = [arr1 objectAtIndex:i];
}
You can't directly do that but you can set arr2 to:
arr2 = #[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
and then
for (UILabel *lbl in arr2) {
NSInteger i=[arr2 indexOfObject:lbl];
lbl.text = [arr1 objectAtIndex:i];
}
Assuming both arrays have the same size, you can do:
NSArray *arr3 = #[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
for (NSUInteger i = 0; i < arr1.count; i++) {
arr3[i].text = arr1[i];
}
If the size of the array received from the server may vary, you can use MIN(arr1.count, arr3.count), i.e. minimum of the two sizes, so you won't get an out-of-bounds exception:
for (NSUInteger i = 0; i < MIN(arr1.count, arr3.count); i++) {
arr3[i].text = arr1[i];
}

Call out of an Array, objectAtIndex

I have an Array with 10 Objects. I take the first Object and put it into my Label with a String.
Then I want to have a Method that increases the objectAtIndex by 1.
This is my Code :
//.m
#interface GameViewController () {
NSInteger _labelIndex;
}
#property (nonatomic) NSArray *playerArray;
#property (nonatomic) NSString *playerLabel;
- (void)viewDidLoad {
[super viewDidLoad];
self.playerArray = [NSArray arrayWithObjects:#"FIRST", #"SECOND", #"THIRD", #"FOURTH", #"FIFTH", #"SIXT", #"SEVENTH", #"EIGTH", #"NINTH", #"TENTH", nil];
_labelIndex = 0;
[self updateTurnLabel];
self.turnLabel.text = [NSString stringWithFormat:#"YOUR %# DRAW?", self.playerLabel];
}
Here I call the Method i another Method:
-(void) flipDraws {
self.boardView.userInteractionEnabled = NO;
[self updateTurnLabel];
CardView *cv1 = (CardView *) self.turnedDrawViews[0];
CardView *cv2 = (CardView *) self.turnedDrawViews[1];
}
This is my Method:
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:_labelIndex % self.playerArray.count]; _labelIndex++;
}
I tried it with a for Loop but nothing happened. I tried it with just set the objectAtIndex:1 but my Method was not called.
What I am doing wrong?
{
int a;
}
- (void)viewDidLoad {
[super viewDidLoad];
a = 0;
self.playerArray = [NSArray arrayWithObjects:#"FIRST", #"SECOND", #"THIRD", #"FOURTH", #"FIFTH", #"SIXT", #"SEVENTH", #"EIGTH", #"NINTH", #"TENTH", nil];
self.playerLabel = [self.playerArray objectAtIndex:a];
self.turnLabel.text = [NSString stringWithFormat:#"YOUR %# DRAW?", self.playerLabel];
}
-(void) updateTurnLabel {
a +=1;
if (!a<[playerArray count])
{
a = 0;
}
self.playerLabel = [self.playerArray objectAtIndex:a];
}
call self.turnLabel.text = [NSString stringWithFormat:#"YOUR %# DRAW?", self.playerLabel]; after [self updateTurnLabel];
What are you adding +1 to in the method objectAtIndex:.
You should be maintaining a variable which tracks the current index being used, then in your method use this :
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:currentIndex+1];
}

Display cell section wise crashes iOS app

I have Tableview with sections in it from A to Z (no of sections are not fixed i calculated dynamically)
I want to display like this:
:
My array values: msg_array=["AajKaCatch","AajKaItem","Anari","Big C Mobiles","Big Flix","BigRock","caksonflowers, ...."]
when i try to display like this in cellForRowAtIndexPath it shows NSInvalidArgumentException
cell.textLabel.text=[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:#"Merchant_Name"];
please help and Thanks In advance.
Your array is like:
array{object,object,object,object,object};
In such a situation, you can't use like:
[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
Because for implementing such one, the [msg_array objectAtIndex:indexPath.section] should return an array.
So implementing this, you need to try like this:
array{array{objects starts with 'A'},array{objects starts with 'B'},array{objects starts with 'C'}};
When you are doing this:
[[[msg_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:#"Merchant_Name"];
You are accessing an element of msg_array as if it was a NSArray, that contains a NSDictionary.
But, inside of msg_array you just have NSStrings.
The structure you are triying to access is:
NSArray -> NSArray -> NSDictionary
And you have
NSArray -> NSString
I have done the same thing for contact info and other things like that using FKRSearchBarTableViewController, see the link and below is mine code for FKRSearchBarTableViewController
- (id)initWithSectionIndexes:(BOOL)showSectionIndexes withDataSource:(NSArray*) dataSource withControllerId:(int) ControllerId forGroup:(int)groupId
{
if ((self = [super initWithNibName:nil bundle:nil])) {
self.title = #"Search Bar";
NSLog(#"%d",groupId);
_groupID = groupId;
_controllerId = ControllerId;
_showSectionIndexes = showSectionIndexes;
_famousPersons = [[NSMutableArray alloc]initWithArray:dataSource];
if (showSectionIndexes) {
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSMutableArray *unsortedSections = [[NSMutableArray alloc] initWithCapacity:[[collation sectionTitles] count]];
for (NSUInteger i = 0; i < [[collation sectionTitles] count]; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
if(ControllerId == 5)
{
for (Person *personName in self.famousPersons) {
// NSInteger index = [collation sectionForObject:[personName objectForKey:#"FirstName"] collationStringSelector:#selector(description)];
NSLog(#"%#",personName.firstName);
NSInteger index = [collation sectionForObject:personName.firstName collationStringSelector:#selector(description)];
[[unsortedSections objectAtIndex:index] addObject:personName];
}
}
else
{
for (NSDictionary *personName in self.famousPersons) {
NSInteger index = [collation sectionForObject:[personName objectForKey:#"FirstName"] collationStringSelector:#selector(description)];
[[unsortedSections objectAtIndex:index] addObject:personName];
}
}
NSMutableArray *sortedSections = [[NSMutableArray alloc] initWithCapacity:unsortedSections.count];
for (NSMutableArray *section in unsortedSections) {
[sortedSections addObject:[NSMutableArray arrayWithArray:[collation sortedArrayFromArray:section collationStringSelector:#selector(description)]]];
}
self.sections = [NSMutableArray arrayWithArray:sortedSections];
}
to make the list more dynamic, solution should be
// given NSArray names = your full list of name
// NSArray indexes = your list of index
NSMutableArray *nameSections = [NSMutableArray arrayWithCapacity:26];
NSMutableArray *filteredIndexes = [NSMutableArray arrayWithCapacity:26];
for (NSString *index in indexes) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"SELF beginswith[c] %#",index];
NSArray *filterNames = [names filteredArrayUsingPredicate:predicate];
if(filterNames.count>0){
[nameSections addObject:filterNames];
[filteredIndexes addObject:index];
}
}
NSLog(#"filteredIndexes %#",filteredIndexes);
NSLog(#"nameSections %#",nameSections);
numOfSection = nameSections.count
numOfRow = [[numOfSection indexOfObject:section]count];
name = [[numOfSection indexOfObject:section]] indexOfObject:row];
// print log
//given indexes array a~z
names (
"a_string",
"a_string2",
"b_string",
"b_string2"
)
filteredIndexes (
a,
b
)
nameSections (
(
"a_string",
"a_string2"
),
(
"b_string",
"b_string2"
)
)

how to retrieve a NSArray from a custom class

I have a custom class which has a integer as a variable.
// addons.h
-(NSMutableArray *) goodDirections:(int)iNumber;
// addons.m
-(NSMutableArray *) goodDirections:(int)iNumber;
{
NSString *gOne = #"one"+iNumber;
NSString *gTwo = #"two"+iNumber;
NSString *gThree = #"three"+iNumber;
NSString *gFour = #"four"+iNumber;
NSMutableArray *goodValues = [NSMutableArray arrayWithObjects:gOne,gTwo,gThree,gFour,nil];
return goodValues;
}
// ViewController.m
addons *directions =[[addons alloc]init];
NSMutableArray *helloTest = [[NSMutableArray alloc]init];
helloTest = [directions goodDirections:3];
NSString *obj1 = [helloTest objectAtIndex:1];
NSLog(#"%#",obj1);
and the custom class has a variable number, when entered returns an array with 4 string values, how do I retrieve the values from the array in my implementation file viewController.m
Make sure you import the custom class in the file you want.Then
NSMutableArray *arr = [theObject simpleMethod:4];
To retrieve any object in arr, you can use objectAtIndex function (for example):
NSString *item = [arr objectAtIndex:1];
please! try below code snip
NSMutableArray *arr = [theObject simpleMethod:4];
obj = [arr objectAtIndex:0];
You can use array object at index.
NSMutableArray *arrayTemp = [self simpleMthod:2];
objTemp = [arrayTemp objectAtIndex:0];
Or You can use
for (int i=0; i<[arrayTemp count]; i++)
{
objTemp = [arrayTemp objectAtIndex:i];
}

Resources