I am new to Xcode and Objective-C programming and need some help.
I am looking to create a basic program for the iOS that uses hierarchal-data and 2 separate UITableViews. I want the second UITableView to be populated by an array that is passed between viewControllers, based on which cell/row is selected in the first UITableView.
The program compiles but I get a SIGABRT error when running the program. Can someone help me fix the SIGABRT and pass the mainArray to the second tableView?
Here is how far I have gotten.
My code:
ArrayTableViewController.h
#interface arrayTableViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *mainArray;
#property (nonatomic, strong) NSMutableArray *secondArray;
#property (nonatomic, strong) NSMutableArray *thirdArray;
#end
ArrayTableViewController.m
#import "ArrayTableViewController.h"
#import "table2.h"
#implementation arrayTableViewController
#synthesize mainArray, secondArray, thirdArray;
-(void) viewDidLoad {
[super viewDidLoad];
mainArray = [[NSMutableArray alloc] initWithObjects: secondArray, thirdArray, nil];
secondArray = [[NSMutableArray alloc] initWithObjects: #"123", #"456", nil];
thirdArray = [[NSMutableArray alloc] initWithObjects: #"78", #"90", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mainArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [mainArray objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
table2 *table2Controller = [[table2 alloc] initWithNibName:#"table2" bundle:nil];
table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1];
[self.navigationController pushViewController:table2Controller animated:YES];
}
#end
table2.h
#import <UIKit/UIKit.h>
#interface table2 : UITableViewController
#property (nonatomic, strong) NSArray *arrayForDisplay;
#end
table2.m
#implementation table3
#synthesize arrayForDisplay;
Then the same cell configuration style that was used in ArrayTableViewController.m
Edits:
After making the necessary changes, when I run the program and select a row, I get a SIGABRT error at the following line.
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([ArrayTableAppDelegate class]));
}
}
What would you recommend? Should I turn off ARC and call my own releases? so that I can get to the second tableView?
First mistake:
In viewDidLoad method, you have created the mainArray with secondArray and thirdArray as elements even before you allocated those arrays.
Second mistake:
In cellForRowAtIndexPath: method:
Check the line cell.textLabel.text = [mainArray objectAtIndex:[indexPath row]];
Actually the textLabel expects a NSString value to set. But you are setting an array.
Edit:
Set the value to textLabel as following:
cell.textLabel.text = [[mainArray objectAtIndex:[indexPath row]] objectAtIndex:0];
Actually this will set the first value of the array. But that it depends on your requirement.
Edit 2:
arrayForDisplay is an array variable but you are setting a string variable to that in the statement
table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1];
Do it as follows:
table2Controller.arrayForDisplay = [mainArray objectAtIndex: [indexPath row]];
Related
guys! Sorry for my bad language.
My app crashes in Update method when I use array which I created in different method InitCats. I create NSMutableArray* Cats and CCSprite* CA in header file in #interface { }.
-(id)init
{
[self InitCats];
[self schedule:#selector(update:) interval:0.0f];
}
-(void)InitCats // This method is work well in -(id)init
{
Cats = [NSMutableArray arrayWithCapacity:NumCats];
for (int a=0; a<NumCats; a++)
{
CCSprite* Cat=[CCSprite spriteWithFile:#"1.png"];
[Cats addObject:Cat];
}
}
-(void) update:(ccTime)delta
{
for (int a=0; a<NumCats; a++)
{
CA = [Cats objectAtIndex:a]; //In this place I have ERROR, app crashes
CA.position = CGPointMake(CA.position.x-1, CA.position.y);
}
}
I guess you pointing to released object. try initialize your array
Cats = [[NSMutableArray alloc] initWithCapacity:NumCats];
and on dealloc
-(void)dealloc
{
[Cats release];
[super dealloc];
}
Ok, the issue is this statement:
Cats = [NSMutableArray arrayWithCapacity:NumCats];
Which will create an auto-released object, however the pointer to this object will be non-nil after it's auto-released, and hence your code is referencing a deallocated object.
You already have a fix:
Cats = [[NSMutableArray alloc] initWithCapacity:NumCats];
and remove the arrayWithCapacity call from within the InitCats method.
(note your capitalization of method and instance variable names is unconventional).
I subclassed CCSpriteBatchNode to make an object that conforms to NSCoding. I was mainly interested in the string name of the CCSpriteBatchNode. After setting break points I realized that the object's string name is always nil. I have a feeling it that my overridden methods might be a contributing factor but I'm not really sure about that. Please see relevant code below:
SpriteBatchNode interface:
#interface SpriteBatchNode: CCSpriteBatchNode {
NSString* batchImageName;
}
SpriteBatchNode implementation:
const NSUInteger defCapacity = 29;
#implementation SpriteBatchNode
#synthesize batchImageName;
+(id)batchNodeWithFile:(NSString*) imageFile
{
return [[self alloc] initWithFile:imageFile capacity:defCapacity];
}
-(id)initWithFile:(NSString *)fileImage {
self = [super initWithFile:fileImage capacity:defCapacity];
if (!self) return nil;
batchImageName = fileImage;
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSString* spriteBatchNodeFileImage = [[aDecoder decodeObjectForKey:#"batchImageName"] copy];
self = [super initWithFile:spriteBatchNodeFileImage capacity:defCapacity];
if (!self) return nil;
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:batchImageName forKey:#"batchImageName"];
}
#end
If you aren't using ARC I see two problems here:
batchImageName string is not retained
batchNodeWithFile: is not sending autorelease to the returned instance
Other than that you're using an unusual init style, the common style is this:
if (self)
{
batchImageName = fileImage;
}
return self;
Checking self for nil, then returning nil if it is, is somewhat redundant.
I have a contact listener that handles contact between two box2d bodies. I am accessing the bodies from the Contacter in the HelloWorldLayer since box2d recommends that contacting bodies should be saved and changes implemented after the timestep. Please see the code below:
Contacter.h:
#import "CCPhysicsSprite.h"
#interface Contacter : CCPhysicsSprite {
}
#property(nonatomic, assign) NSMutableArray* arrayOfBodies;
#property(nonatomic, assign) CCPhysicsSprite* spriteToDestroy;
-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite:(CCPhysicsSprite*)twoPhysicsSprite;
#end
Contacter.mm:
#import "Contacter.h"
#import "Box2D.h"
#implementation Contacter
#synthesize arrayOfBodies = _arrayOfBodies;
#synthesize spriteToDestroy = _spriteToDestroy;
-(void)destroyBodies:(b2Body*)body {
_arrayOfBodies = [[NSMutableArray alloc] init];
NSValue *bodyValue = [NSValue valueWithPointer:body];
[_arrayOfBodies addObject:bodyValue];
}
-(void)physicsSpritesContact:(CCPhysicsSprite*)onePhysicsSprite otherSprite: (CCPhysicsSprite*)twoPhysicsSprite; {
int firstTag = onePhysicsSprite.tag;
int secondTag = twoPhysicsSprite.tag;
if (((firstTag == 90) && (secondTag == 101 )) || ((firstTag == 101) && (secondTag == 90))) {
if (tag1 == 90) {
[self destroyBodies:onePhysicsSprite.b2Body];// adds body to array to be destroyed
spriteToDestroy = onePhysicsSprite; // taking note of sprite to be destroyed
}
else if (tag2 == 90) {
[self destroyBodies:twoPhysicsSprite.b2Body];
spriteToDestroy = twoPhysicsSprite;
}
}
}
The following method within HelloWorldLayer.mm is called in the update method:
-(void)removeDestroyedBodiesAndSprites {
bodyContact = [Contacter node];
if ([bodyContact arrayOfBodies]) {
for (NSValue* bodyValue in [bodyContact arrayOfBodies]) {
b2Body *removeBody;
removeBody = (b2Body*)[bodyValue pointerValue];
world->DestroyBody(removeBody);
removeBody = NULL;
[self removeChild:[bodyContact spriteToDestroy]];
}
}
}
There is contact but the sprite is not removed and body is not destroyed in removeDestroyedBodiesAndSprites. After testing with a CCLOG I found that the for loop was not satisfied meaning that the arrayOfBodies could be null. Which is surprising since the contact was established. I would appreciate your assistance.
UPDATED
Below is the contact listener:
TestContactListener.h:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "GameObjects.h"
#import "Contacter.h"
class TestContactListener : public b2ContactListener {
public:
Contacter* contacter;
void BeginContact(b2Contact* contact);
};
TestContactListener.mm:
#import "TestContactListener.h"
void TestContactListener:: BeginContact(b2Contact *contact)
{
contacter = [Contacter node];
b2Fixture *fixtureA = contact->GetFixtureA();
b2Fixture *fixtureB = contact->GetFixtureB();
b2Body *fixtureABody = fixtureA->GetBody();
b2Body *fixtureBBody = fixtureB->GetBody();
CCPhysicsSprite* physicsSprite = (CCPhysicsSprite*)fixtureABody->GetUserData();
CCPhysicsSprite* physicsSprite2 = (CCPhysicsSprite*)fixtureBBody->GetUserData();
[contacter physicsSpritesContact:physicsSprite otherSprite:physicsSprite2];
}
Move this from the destroyBodies method to an init method, so it is only called once:
_arrayOfBodies = [[NSMutableArray alloc] init];
Take a good look at your destroyBodies method. You create a new array, replacing any existing array, every time you call it. Therefore you will only destroy the last body you passed in to that method.
If you are not using ARC you'll also leak all the replaced arrays.
I have an NSMutable array that I want to add Sprites to so that I can check them if they've hit the wall. I use this code to do so:
NSString *bulletName = [NSString stringWithFormat:#"tank%d_bullet.png", _type];
bullet = [CCSprite spriteWithSpriteFrameName:bulletName];
bullet.tag = _type;
bullet.position = ccpAdd(self.position, ccpMult(_shootVector, _turret.contentSize.height));
CCMoveBy * move = [CCMoveBy actionWithDuration:duration position:actualVector];
CCCallBlockN * call = [CCCallBlockN actionWithBlock:^(CCNode *node) {
[node removeFromParentAndCleanup:YES];
}];
if (!bulletIsGone) {
[self schedule:#selector(updator:) interval:0.01];
}
else {
[self unschedule:#selector(updator:)];
}
[bullet runAction:[CCSequence actions:move, call, nil]];
[_layer.batchNode addChild:bullet];
[bulletsArray addObject:bullet];
if ([bulletsArray objectAtIndex:0] == nil) {
NSLog(#"HELP");
}
NSLog(#"%#", [bulletsArray objectAtIndex:0]);
}
-(void)updator: (ccTime) dt{
for(CCSprite *bulletz in bulletsArray){
NSLog(#"this is the for loop");
CGRect rect1 = CGRectMake(bulletz.position.x - bulletz.contentSize.width/2, bulletz.position.y - bulletz.contentSize.height/2, 20, 20);
if ([_layer isWallAtRect:rect1]) {
NSLog(#"bulletHitWall");
[_layer.batchNode removeChild:bulletz cleanup:NO];
bulletIsGone = YES;
}
}
}
However, when I build and run, I get the console output of '(null)' and 'HELP.' The method before the 'updator' is called from touchesEnded. Can someone see what I'm doing wrong?
Thank you!
Do you initialise the array? That would seem like the most likely reason
Try this in your viewDidLoad method...
- (void)viewDidLoad
{
[super viewDidLoad];
bulletsArray = [NSMutableArray alloc] init];
}
Since NSMutableArray cannot hold nil objects, the only way the condition
[bulletsArray objectAtIndex:0] == nil
could evaluate to true is that bulletsArray is nil. You need to make sure that the array is properly allocated. A typical place to do it is the designated initializer of your class.
Why do you want to add bullets to another array? You already have a batch node that contains them all which is _layer.children.
Are you sure that the array itself (bulletsArray) is not nil? where is it initialized?
Finally you should consider looping with CCARRAY_FOREACH which is more performant.
I created OCUnit test in concordance with "iPhone Development Guide". Here is the class I want to test:
// myClass.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface myClass : NSObject {
UIImage *image;
}
#property (readonly) UIImage *image;
- (id)initWithIndex:(NSUInteger)aIndex;
#end
// myClass.m
#import "myClass.m"
#implementation myClass
#synthesize image;
- (id)init {
return [self initWithIndex:0];
}
- (id)initWithIndex:(NSUInteger)aIndex {
if ((self = [super init])) {
NSString *name = [[NSString alloc] initWithFormat:#"image_%i", aIndex];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"png"];
image = [[UIImage alloc] initWithContentsOfFile:path];
if (nil == image) {
#throw [NSException exceptionWithName:#"imageNotFound"
reason:[NSString stringWithFormat:#"Image (%#) with path \"%#\" for current index (%i) wasn't found.",
[name autorelease], path, aIndex]
userInfo:nil];
}
[name release];
}
return self;
}
- (void)dealloc {
[image release];
[super dealloc];
}
#end
And my unit-test (LogicTests target):
// myLogic.m
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import "myClass.h"
#interface myLogic : SenTestCase {
}
- (void)testTemp;
#end
#implementation myLogic
- (void)testTemp {
STAssertNoThrow([[myClass alloc] initWithIndex:0], "myClass initialization error");
}
#end
All necessary frameworks, "myClass.m" and images added to target. But on build I have an error:
[[myClass alloc] initWithIndex:0] raised Image (image_0) with path \"(null)\" for current index (0) wasn't found.. myClass initialization error
This code (initialization) works fine in application itself (main target) and later displays correct image. I've also checked my project folder (build/Debug-iphonesimulator/LogicTests.octest/) - there are LogicTests, Info.plist and necessary image files (image_0.png is one of them).
What's wrong?
Found only one solution for this problem.
When I build my unit-tests, the main bundle's path is not equal to my project's bundle (created .app file). Moreover, it's not equal to LogicTests bundle (created LogicTests.octest file).
Main bundle for unit-tests is something like /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/Developer/usr/bin. And that's why program can't find necessary resources.
And the final solution is to get direct bundles:
NSString *path = [[NSBundle bundleForClass:[myClass class]] pathForResource:name ofType:#"png"];
instead of
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"png"];