Threads seem to be terminating the app


#1

so i got to the build and run the app the second time but when i build and run the app then the main.m comes up with a highlighted green line that says
"return UIApplicationMain(argc, argv, nil, NSStringFromclass([HomepwnerAppDelegate class]));"
that is highlighted green
and then when i press the play button again then it terminates my app the output say terminate called throwing an exception

please help


#2

The main function is the startpoint of every app. If you have edited it, you should revert it, since an iOS Application rarely needs any editing in the actual main function.
Either, an exception is causing the application to return to the main method, or your application just finishes running. Usually, an App terminates like you describe because there is some exception. The output also makes it clear.
Just put the output to this forum, and your code too, that will help finding the problem.


#3

i did not edit the main code i have left is alone
my codes
BNRItem.h

[code]
#import <Foundation/Foundation.h>

@interface BNRItem : NSObject

  • (id)randomItem;
  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber;

@property (nonatomic, strong) BNRItem *containedItem;
@property (nonatomic, weak) BNRItem *container;

@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly, strong) NSDate *dateCreated;

@end[/code]
BNRItem.m

[code]
#import “BNRItem.h”

@implementation BNRItem
@synthesize itemName;
@synthesize containedItem, container, serialNumber, valueInDollars, dateCreated;

  • (id)randomItem
    {
    // Create an array of three adjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”,
    @“Rusty”,
    @“Shiny”, nil];
    // Create an array of three nouns
    NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”,
    @“Spork”,
    @“Mac”, nil];
    // Get the index of a random adjective/noun from the lists
    // Note: The % operator, called the modulo operator, gives
    // you the remainder. So adjectiveIndex is a random number
    // from 0 to 2 inclusive.
    NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];

    // Note that NSInteger is not an object, but a type definition
    // for “unsigned long”

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
    [randomAdjectiveList objectAtIndex:adjectiveIndex],
    [randomNounList objectAtIndex:nounIndex]];
    int randomValue = rand() % 100;
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10];
    // Once again, ignore the memory problems with this method
    BNRItem *newItem =
    [[self alloc] initWithItemName:randomName
    valueInDollars:randomValue
    serialNumber:randomSerialNumber];
    return newItem;
    }

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber
    {
    // Call the superclass’s designated initializer
    self = [super init];

    // Did the superclass’s designated initializer succeed?
    if(self) {
    // Give the instance variables initial values
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];
    }

    // Return the address of the newly initialized object
    return self;
    }

  • (id)init
    {
    return [self initWithItemName:@“Possession"
    valueInDollars:0
    serialNumber:@”"];
    }

  • (void)setContainedItem:(BNRItem *)i
    {
    containedItem = i;
    [i setContainer:self];
    }

  • (NSString *)description
    {
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
    itemName,
    serialNumber,
    valueInDollars,
    dateCreated];
    return descriptionString;
    }

  • (void)dealloc
    {
    NSLog(@“Destroyed: %@”, self);
    }

@end[/code]
ItemsViewController.h

[code]#import <Foundation/Foundation.h>

@interface ItemsViewController : UITableViewController

@end[/code]
ItemsViewController.m

[code]#import “ItemsViewController.h”
#import “BNRItemStore.h”
#import “BNRItem.h”

@implementation ItemsViewController

  • (id)init
    {
    // Call the superclass’s designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
    for (int i = 0; 1 < 5; i++) {
    [[BNRItemStore sharedStore] createItems];
    }
    }
    return self;
    }

  • (id)initWithStyle:(UITableViewStyle)style
    {
    return [self init];
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [[[BNRItemStore sharedStore] allItems] count];
    }

  • (UITableViewCell *)tableView:(UITableViewCell *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Create an instance of UITableViewCell, with default apperance
    UITableViewCell *cell =
    [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“UITableViewCell”];

    // Set the text on the cell with the descripition of the item that is at the nth index on items, where n = row this cell will appear in on the tableView
    BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];

    [[cell textLabel] setText:[p description]];

    return cell;

}

@end[/code]

HomepwnerAppDelegate.h

[code]#import <UIKit/UIKit.h>

@interface HomepwnerAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end[/code]

HomepwnerAppDelegate.m

[code]#import “HomepwnerAppDelegate.h”
#import “ItemsViewController.h”

@implementation HomepwnerAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    // Create a ItemsViewController
    ItemsViewController *itemsViewController = [[ItemsViewController alloc] init];

    // Place ItemsViewController’s table view in the window hierachy
    [[self window] setRootViewController:itemsViewController];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)applicationWillResignActive:(UIApplication *)application
    {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application
    {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application
    {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application
    {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application
    {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end[/code]
BNRItemStore.h

[code]
#import <Foundation/Foundation.h>

@class BNRItem;

@interface BNRItemStore : NSObject
{
NSMutableArray *allItems;
}
// Notice that this is a class method and prefixed with a + instead of a -

  • (BNRItemStore *)sharedStore;
  • (NSArray *)allItems;
  • (NSArray *)createItems;

@end[/code]
BNRItemStore.m

[code]
#import “BNRItemStore.h”
#import “BNRItem.h”

@implementation BNRItemStore

  • (BNRItemStore *)sharedStore
    {
    static BNRItemStore *sharedStore = nil;
    if (!sharedStore)
    sharedStore = [[super allocWithZone:nil] init];

    return sharedStore;
    }

  • (id)allocWithZone:(NSZone *)zone
    {
    return [self sharedStore];
    }

  • (id)init
    {
    self = [super init];
    if (self) {
    allItems = [[NSMutableArray alloc] init];
    }

    return self;
    }

  • (NSArray *)allItems
    {
    return allItems;
    }

  • (BNRItem *)createItem
    {
    BNRItem *p = [BNRItem randomItem];
    [allItems addObject:p];
    return p;
    }

@end[/code]
here is the output
2013-12-09 10:23:07.605 Homepwner[3468:907] -[BNRItemStore
createItems]: unrecognized selector sent to instance 0x760f620
2013-12-09 10:23:07.607 Homepwner[3468:907] *** Terminating app
due to uncaught exception ‘NSInvalidArgumentException’, reason:
’-[BNRItemStore createItems]: unrecognized selector sent to instance 0x760f620’
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x2733 0x22e1
0x12157 0x12747 0x1394b 0x24cb5 0x25beb 0x17698 0x1beedf9
0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b
0x1317a 0x14ffc 0x211d 0x2045)
libc++abi.dylib: terminate called throwing an exception
(lldb)


#4

Your mistake is to send „createItems” instead of „createItem” (without s) to the BNRItemStore.
The error message „[BNRItemStore createItems]: unrecognized selector sent to instance …” says that exactly. The „selector” is the method name you call, and the line, in which you call it is printed before, which is [BNRItemStore createItems]. Now you just have to search for the place where you write „createItems” and correct it.

( You wrote createItems in itemsViewController: )

@implementation ItemsViewController

  • (id)init
    {
    // Call the superclass’s designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
    for (int i = 0; 1 < 5; i++) {
    [[BNRItemStore sharedStore] createItems];
    }
    }
    return self;
    }


#5

Grrr
so now there is an error and i only got one step further can anybody help me again?? please