I can't figure out why the problem described here is happening.
This my student.h:
#import <Foundation/Foundation.h>
#interface Student : NSObject
#property NSString* fName;
#property NSString* lName;
#property NSString* studentId;
#property NSString* phoneNum;
#property NSString* degree;
-(id) init: (NSString*)fName lName:(NSString*)lName sId:(NSString*)sId pn:(NSString*)pn degree: (NSString*) degree;
#end
This is my student.m:
#import "Student.h"
#implementation Student
-(id) init: (NSString*)fName lName:(NSString*)lName sId:(NSString*)sId pn:(NSString*)pn degree: (NSString*) degree
{
self = [super init];
if(self){
self.fName = fName;
self.lName = lName;
self.studentId = sId;
self.phoneNum = pn;
self.degree = degree;
}
return self;
}
#end
And this is my main:
#import <Foundation/Foundation.h>
#import "Student.h"
#import "StudentDB.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
Student* ignacio =[[Student alloc] init: #"ignacio" lName: #"cooper" sId: #"123456" pn:#"01234" degree:#"Computer science"];
[[StudentDB instance] addStudents: ignacio];
}
return 0;
}
The error is happening in this line:
Student* ignacio =[[Student alloc] init: #"ignacio" lName: #"cooper" sId: #"123456" pn:#"01234" degree:#"Computer science"];
Here's the entire error:
no visible #interface for 'Student' declares the selector 'init:lName:sId:pn:degree'
Without the StudentDB it works fine. It appears the trouble is there, but I can't find it. Can anyone give me an idea on how I can solve this?
Here are my StudentDB.h and StudentDB.m:
#import "StudentDB.h"
#implementation StudentDB
static StudentDB* instance = nil;
-(id)init
{
self = [super init];
if(self){
self.students = [[NSMutableArray alloc] init];
}
return self;
}
+ (StudentDB*) instance
{
if(instance == nil){
instance = [[StudentDB alloc] init];
}
return instance;
}
-(void) addStudents:(Student *)s
{
[self.students addObject: s];
}
-(Student*) getStudent:(NSString*)sId
{
for(Student* s in self.students)
{
if(s.studentId == sId)
return s;
}
return nil;
}
-(void) deleteStudent:(NSString*)sId
{
for(Student* s in self.students)
{
if(s.studentId == sId){
[self.students removeObject:s];
break;
}
}
}
-(void) updateStudent:(Student*)s
{
[self deleteStudent: s.studentId];
[self addStudents:s];
}
#end
#import <Foundation/Foundation.h>
#import "Student.h"
#interface StudentDB : NSObject
#property NSMutableArray* students;
+(StudentDB*) instance;
-(id)init;
-(void) addStudents:(Student *)s;
-(Student*) getStudent:(NSString*)sId;
-(void) deleteStudent:(NSString*)sId;
-(void) updateStudent:(Student*)s;
#end
Related
I'm trying to implement KVOController in my app. I managed to get it working on custom UISliders, but I can't seem to get it working on a custom NSObject. This is the object:
Restaurant.h
#import <UIKit/UIKit.h>
#interface Restaurant : NSObject
#property (nonatomic, copy, readonly) NSString *name, *tagline;
#property (nonatomic, copy, readonly) UIColor *uicolor;
- (id)initWithName:(NSString *)main;
- (void)setName:(NSString *)title;
- (void)changeColor:(UIColor)color;
#end
Restaurant.m
#implementation Restaurant
- (id)initWithName:(NSString *)main {
self = [super init];
if (self)
{
_name = main;
}
return self;
}
- (void)setName:(NSString *)title {
_name = title;
}
- (void)changeColor:(UIColor)color {
_uicolor = color;
}
#end
So, I introduced this listener into my code:
- (void)viewDidLoad {
[super viewDidLoad];
self.restaurant = [[Restaurant alloc] initWithName:#"test"];
[self.restaurant changeColor:[UIColor blueColor]];
FBKVOController *KVOController = [FBKVOController controllerWithObserver:self];
self.KVOController = KVOController;
[self.KVOController observe:self.restaurant keyPath:#"name" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew block:^(id observer, id object, NSDictionary *change) {
NSLog(#"Restaurant changed!");
}];
}
- (void)someOtherMethod:(id)sender {
[self.restaurant setName:#"Another test"];
[self.restaurant changeColor:[UIColor redColor]];
}
However, this isn't logging anything for this, nor when I change the keyPath to #"uicolor". What am I doing wrong?
I have my classe which contains my data Data
I created my object in my first View called ViewController
I will create others viewControllers and I want to read and write datas in the object "man1" created in my ViewController.
How can I do that ?
Thank you very much.
This is my code so far:
Data.H
#import <Foundation/Foundation.h>
#interface Data : NSObject
{
NSString *name;
int age;
NSString *city;
}
- (id)initWithName:(NSString *)aName ;
- (NSString*) name;
- (int) age;
- (NSString*) city;
//- (void) setPrenom:(NSString*) prenom;
- (void) setName:(NSString*) newName;
- (void) setAge:(int) newAge;
- (void) setCity:(NSString*) newCity;
#end
Data.m
#import "Data.h"
#implementation Data
- (id)initWithName:(NSString *)aName
{
if ((self = [super init]))
{
self.name = aName;
}
return self;
}
//getter
- (NSString*) name
{
return name;
}
- (int) age{
return age;
}
- (NSString*) city{
return city;
}
//setter
- (void) setName:(NSString*)newName
{
name = newName;
}
- (void) setAge:(int) newAge
{
age = newAge;
}
- (void) setCity:(NSString *)newCity
{
city = newCity;
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "Data.h"
#interface ViewController : UIViewController
{
int testint;
}
#property (readwrite) Data *man1;
#property (weak, nonatomic) IBOutlet UILabel *labelAff;
#end
ViewController.m
#import "ViewController.h"
#import "Data.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize man1 = _man1;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString * name1 = #"Bob";
_man1 = [[Data alloc]initWithName:name1 ];
NSLog(#" %# ", _man1.name);
[_man1 setAge:29];
NSLog(#" %d ", _man1.age);
[_man1 setCity:#"Tapei"];
_labelAff.text = [_man1 city];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Your approach is not going to work, because you allocate a new instance of Data each time a view is loaded, and because each view controller gets its own Data object.
One approach to fixing this is making your Data class a singleton. Your view controllers will be accessing a single instance of Data, ensuring that the information is shared among the view controllers:
Data.h
#interface Data : NSObject
{
NSString *name;
int age;
NSString *city;
}
- (id)initWithName:(NSString *)aName ;
- (NSString*) name;
- (int) age;
- (NSString*) city;
- (void) setName:(NSString*) newName;
- (void) setAge:(int) newAge;
- (void) setCity:(NSString*) newCity;
+(Data*)instance;
#end
Data.m
#implementation Data
-(id)initWithName:(NSString *)aName {
if(self=[super init]) {
...
}
return self;
}
+(Data*)instance {
static dispatch_once_t once;
static Data *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] initWithName: ...];
});
return sharedInstance;
}
#end
Please help....I'm losing my mind trying to figure out this problem.
I'm fairly new to iOS so don't go too hard on me if it's something obvious! ;)
I'm using xcode 4.6 and targeting iPhone6.1 Simulator.
I get the following error when starting up my app:
EXC_BAD_ACCESS code = 2
There are hundres of threads appearing in Debug Navigator which leads to to believe there is some sort of infinite loop somewhere (I just cannot see where).
The error occurs beside (id)init in PlayingCardDeck.m after entering it from ViewController.m at line:
Card *card = [self.deck drawRandonCard];
ViewConrtoller:
#import "ViewController.h"
#import "PlayingCardDeck.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
#property (nonatomic) int flipCount;
#property (strong, nonatomic) Deck *deck;
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
#end
#implementation ViewController
#synthesize deck = _deck;
- (IBAction)flipCard:(UIButton *)sender {
sender.selected = !sender.isSelected;
self.flipCount++;
}
- (void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
self.flipsLabel.text = [NSString stringWithFormat:#"Flips: %d", self.flipCount];
}
- (Deck *)deck
{
if (!_deck) _deck = [[PlayingCardDeck alloc] init];
return _deck;
}
- (void)setCardButtons:(NSArray *)cardButtons
{
_cardButtons = cardButtons;
for (UIButton *cardButton in cardButtons)
{
Card *card = [self.deck drawRandonCard];
[cardButton setTitle:card.contents forState:UIControlStateSelected];
}
}
#end
Deck.m
#import "Deck.h"
#interface Deck()
#property (strong, nonatomic) NSMutableArray *cards;
#end
#implementation Deck
- (NSMutableArray *)cards
{
if (!_cards) _cards = [[NSMutableArray alloc] init];
return _cards;
}
- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
if (atTop)
{
[self.cards insertObject:card atIndex:0];
}
else
{
[self.cards addObject:card];
}
}
- (Card *)drawRandonCard
{
Card *randomCard = nil;
if (self.cards.count)
{
unsigned index = arc4random() % self.cards.count;
randomCard = self.cards[index];
[self.cards removeObjectAtIndex:index];
}
return randomCard;
}
#end
PlayingCardDeck.m
#import "PlayingCardDeck.h"
#import "PlayingCard.h"
#implementation PlayingCardDeck
- (id)init
{
self = [self init];
if (self)
{
for (NSString *suit in [PlayingCard validSuits])
{
for (NSUInteger rank=1; rank <= [PlayingCard maxRank]; rank++)
{
PlayingCard *card = [[PlayingCard alloc] init];
card.suit = suit;
card.rank = rank;
[self addCard:card atTop:YES];
}
}
}
return self;
}
#end
In PlayerCardDeck.m self = [self init] should be self = [super init]. That's causing the infinite loop.
I have a singleton object in iOS that when instantiated parses a CSV file and then holds the results. I would like to make this object universally accessible and I would like it to not be released from memory until the app quits. I am running ARC so I cannot do manual retains.
Is there a way I can do this so it will work with ARC?
Header File:
#import <Foundation/Foundation.h>
#import "CHCSV.h"
#import "RCParserObject.h"
#interface ParserStore : NSObject <CHCSVParserDelegate>
{
// CSV Variables
RCParserObject *item;
NSMutableArray *data;
NSMutableArray *parsedData;
int fields;
bool open;
}
#property (atomic, retain) RCParserObject *item;
#property (atomic, retain) NSMutableArray *data;
#property (atomic, retain) NSMutableArray *parsedData;
#property (atomic) int fields;
#property (atomic) bool open;
+ (ParserStore *) defaultStore;
- (void) parseCSVFile:(NSString*)file;
- (void) categorizeData;
Implementation File
#import "ParserStore.h"
#import "RCParserObject.h"
#import "RCDetailItem.h"
static ParserStore *defaultStore;
#implementation ParserStore
#synthesize item, data, parsedData, fields, open;
# pragma mark -
# pragma mark Singleton Methods
+ (ParserStore *) defaultStore
{
if (!defaultStore)
{
defaultStore = [[super allocWithZone:NULL] init];
}
return defaultStore;
}
+ (id) allocWithZone:(NSZone *)zone
{
return [self defaultStore];
}
- (id) init
{
NSLog(#"Running init on parser store");
if (defaultStore)
{
NSLog(#"Self data count is %d, right before return", self.parsedData.count);
return defaultStore;
}
// NSLog(#"This better only happen once");
self = [super init];
[self setParsedData:[[NSMutableArray alloc] init]];
[self parseCSVFile:#"ContentNoPathFileExt2ASCII"];
[self categorizeData];
// NSLog(#"Self data count is %d when first created", self.parsedData.count);
return self;
}
#property (atomic, retain) RCParserObject *item;
#property (atomic, retain) NSMutableArray *data;
#property (atomic, retain) NSMutableArray *parsedData;
#property (atomic) int fields;
#property (atomic) bool open;
+ (ParserStore *) defaultStore;
- (void) parseCSVFile:(NSString*)file;
- (void) categorizeData;
#end
+(MySingleton *)singleton {
static dispatch_once_t pred;
static MySingleton *shared = nil;
dispatch_once(&pred, ^{
shared = [[MySingleton alloc] init];
shared.someVar = someValue; // if you want to initialize an ivar
});
return shared;
}
From anywhere:
NSLog(#"%#",[MySingleton singleton].someVar);
Note that your iOS app already has a singleton that you can access anywhere:
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
a simple singleton would be something like... in the .h:
#interface Foo : NSObject
+ (Foo *)sharedInstance;
#end
and in the .m:
static Foo *_foo = nil;
#implementation Foo
+ (Foo *)sharedInstance {
if (!_foo)
_foo = [[Foo alloc] init];
return _foo;
}
#end
I have gone through a few tutorials including the sample app included
with Three20 and cannot figure out why photos aren't showing up in my
TTPhotoViewController. I actually find it pretty hard to debug.
Below is the code I have. Any thoughts on why images will not load
and how to debug it would be great. I get a completely black view in
between my bottom tabbar and upper nav bar. I also see left and right
arrows overlayed on the black view which seems to be for navigating
photos although I thought it was supposed to display a thumbnail
gallery.
// A TTPhoto class
// Photo.h
#import <Foundation/Foundation.h>
#import <Three20/Three20.h>
#interface Photo : NSObject <TTPhoto> {
NSString *_caption;
NSString *_urlLarge;
NSString *_urlSmall;
NSString *_urlThumb;
id <TTPhotoSource> _photoSource;
CGSize _size;
NSInteger _index;
}
#property (nonatomic, copy) NSString *caption;
#property (nonatomic, copy) NSString *urlLarge;
#property (nonatomic, copy) NSString *urlSmall;
#property (nonatomic, copy) NSString *urlThumb;
#property (nonatomic, assign) id <TTPhotoSource> photoSource;
#property (nonatomic) CGSize size;
#property (nonatomic) NSInteger index;
- (id)initWithCaption:(NSString *)caption urlLarge:(NSString
*)urlLarge urlSmall:(NSString *)urlSmall urlThumb:(NSString *)urlThumb
size:(CGSize)size;
#end
// Photo.m
#import "Photo.h"
#implementation Photo
#synthesize caption = _caption;
#synthesize urlLarge = _urlLarge;
#synthesize urlSmall = _urlSmall;
#synthesize urlThumb = _urlThumb;
#synthesize photoSource = _photoSource;
#synthesize size = _size;
#synthesize index = _index;
- (id)initWithCaption:(NSString *)caption urlLarge:(NSString
*)urlLarge urlSmall:(NSString *)urlSmall urlThumb:(NSString *)urlThumb
size:(CGSize)size {
if ((self = [super init])) {
self.caption = caption;
self.urlLarge = urlLarge;
self.urlSmall = urlSmall;
self.urlThumb = urlThumb;
self.size = size;
self.index = NSIntegerMax;
self.photoSource = nil;
}
return self;
}
- (void) dealloc {
self.caption = nil;
self.urlLarge = nil;
self.urlSmall = nil;
self.urlThumb = nil;
[super dealloc];
}
#pragma mark TTPhoto
- (NSString*)URLForVersion:(TTPhotoVersion)version {
switch (version) {
case TTPhotoVersionLarge:
return _urlLarge;
case TTPhotoVersionMedium:
return _urlLarge;
case TTPhotoVersionSmall:
return _urlSmall;
case TTPhotoVersionThumbnail:
return _urlThumb;
default:
return nil;
}
}
#end
// A TTPhotoSource class
// PhotoSet.h
#import <Foundation/Foundation.h>
#import "Three20/Three20.h"
#interface PhotoSet : TTURLRequestModel <TTPhotoSource> {
NSString *_title;
NSArray *_photos;
NSArray* _tempPhotos;
NSTimer* _fakeLoadTimer;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, retain) NSArray *photos;
- (id) init;
#end
// PhotoSet.m
#import "PhotoSet.h"
#import "Photo.h"
#implementation PhotoSet
#synthesize title = _title;
#synthesize photos = _photos;
- (id) init {
_title = #"Test photo album";
_photos = [[NSArray alloc] initWithObjects:
[[[Photo alloc] initWithCaption:#"coming soon"
urlLarge:#"http://farm5.static.flickr.com/
4066/4653156849_0905e6b58e_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4066/4653156849_0d15f0e3f0_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4066/4653156849_0d15f0e3f0_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
[[[Photo alloc] initWithCaption:#"coming soon 2"
urlLarge:#"http://farm5.static.flickr.com/
4023/4653774402_05e6acd995_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
[[[Photo alloc] initWithCaption:#"coming soon 2"
urlLarge:#"http://farm5.static.flickr.com/
4023/4653774402_05e6acd995_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
[[[Photo alloc] initWithCaption:#"coming soon 2"
urlLarge:#"http://farm5.static.flickr.com/
4023/4653774402_05e6acd995_o.jpg"
urlSmall:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
urlThumb:#"http://farm5.static.flickr.com/
4009/4653157237_c2f5f59e0d_s.jpg"
size:CGSizeMake(220, 112)] autorelease],
nil];
for (int i = 0; i < _photos.count; ++i) {
id<TTPhoto> photo = [_photos objectAtIndex:i];
if ((NSNull*)photo != [NSNull null]) {
NSLog(#"in here 65434");
photo.photoSource = self;
photo.index = i;
}
}
return self;
}
- (void) dealloc {
self.title = nil;
self.photos = nil;
[super dealloc];
}
#pragma mark TTModel
- (BOOL)isLoading {
return NO;
}
- (BOOL)isLoaded {
return YES;
}
#pragma mark TTPhotoSource
- (NSInteger)numberOfPhotos {
return _photos.count;
}
- (NSInteger)maxPhotoIndex {
return _photos.count-1;
}
- (id<TTPhoto>)photoAtIndex:(NSInteger)photoIndex {
if (photoIndex < _photos.count) {
return [_photos objectAtIndex:photoIndex];
} else {
return nil;
}
}
#end
// A TTPhotoViewController
// EventDetailViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <Three20/Three20.h>
#import "PhotoSet.h"
#class Event;
#interface EventDetailViewController :
TTPhotoViewController<UINavigationControllerDelegate,
UIImagePickerControllerDelegate> {
NSArray *photos;
Event *event;
PhotoSet *_photoSet;
}
#property (nonatomic, retain) NSArray *photos;
#property (nonatomic, retain) Event *event;
#property (nonatomic, retain) PhotoSet *photoSet;
- (id)initWithEvent:(Event *)e;
// EventDetailViewController.m
#import "EventDetailViewController.h"
#import "Event.h"
#import "PhotoSet.h"
#import "Photo.h"
#implementation EventDetailViewController
#synthesize photos;
#synthesize event;
#synthesize photoSet = _photoSet;
#pragma mark -
#pragma mark Initialization
- (void)viewDidLoad {
self.photoSet = [[PhotoSet alloc] init];
self.photoSource = self.photoSet;
}
- (id)initWithEvent:(Event *)e {
if (self) {
self.event = e;
}
return self;
}
#end
You may find an answer here: http://www.raywenderlich.com/1430/how-to-use-the-three20-photo-viewer