Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a location app that already successfully asks the user to enable location services, and then can show them their coordinates on a button press as well. So I decided to play around with everything that is available in the CLLocationManager reference provided by xcode.
I decided to setup a bool method called "locationServicesEnabled". It returns a value of YES(1) or NO(0). I declared the method and then went to implement it. I am trying to have NSLog print the bool result out to the console when you open the app.
Here is how I declared the BOOL method in my ViewController.m file:
#interface ViewController () <CLLocationManagerDelegate>
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
+ (BOOL)locationServicesEnabled;
#end
And here is how I implemented the BOOL method in ViewController.m:
+ (BOOL)locationServicesEnabled{
[self locationServicesEnabled];
NSLog(#"%hhd", self.locationServicesEnabled);
return 0;
}
Why do you want to create an extra method? You could minimize the risk of errors by using already available ones: NSLog(#"Location services enabled: %d",[CLLocationManager locationServicesEnabled]);
I think your issue might be the fact that you are calling your function inside its self:
+ (BOOL)locationServicesEnabled
{
[self locationServicesEnabled]; <- should this be here
NSLog(#"%hhd", self.locationServicesEnabled);
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am new to programming, but I can't seem to get this to work.
When I try to run the method from another method every thing stops
-(void)rotateMmovment {
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}
First you should check either your method is running or not via using NSLog.its does't seems that you are facing problem due to calling method which have empty body
-(void)rotateMmovment {
NSLog(#"My method is running");
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
how can I add an object in an array by clicking a button
I use this methode :
- (void)insertNewObject:(id)sender
{
[orderListe addObject:[[DataOrder alloc]initWithName:#"Michael" price:18 taille:#"junior" supplement:#"boeuf"]];
}
When I do that my application crash and say :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DataOrder initWithName:]: unrecognized selector sent to instance 0x8aa4fa0'
What can i do to simply add an object to an array ?
Thank you
The error your getting has to do with the DataOrder object. During it's initialization it ran into an error. You're sending it some value it isn't expecting.
Perhaps try and separate the line where you alloc the Data Order object and try to add it to the array?
DataOrder *do = [[DataOrder alloc] initWithName....];
[orderListe addObject: do];
this will let you see where you messed up?
Of course that you can add an object to an array. The exception is thrown because the method initWithName:price: ... is not implemented in the DataOrder class.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a UIButton which when pressed passes an integer to a simple method I have set up. However I keep on getting this error:
Implicit conversion of 'int' to 'id' is disallowed with ARC
Here is my code:
[self performSelector:#selector(show:) withObject:prev_image afterDelay:2.0];
The reason I'm not just doing [self show:prev_image] is because I want a delay before the method is called.
Thanks for your time, Dan.
You have two choices:
Change the show: method to take an NSNumber and then wrap prev_image in an NSNumber or
Use dispatch_after.
Code:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self show:prev_image];
});
prev_image is an int where an 'object' must be passed into the performSelector method.
I would advise that you do this:
[self performSelector:#selector(show:) withObject:#(prev_image) afterDelay:2.0f];
I would also recommend you change prev_image to prevImage whilst programming in Objective-C simply for style.
Using dispatch_after is not necessary here and you almost certainly want to stay as high level as possible when tackling problems in iOS development.
I apologise for being unclear.
You will also want to change the method signature and implementation of show:
- (void)show:(NSNumber *)number
{
NSInteger integerNumber = [number integerValue];
}
One solution is to use [NSNumber numberWithInt:prev_image] instead of prev_image. Also you'll need to change the show: method to to take an NSNumber instead of an int.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
As I get to some performance issues in my app and find that I use database access in a bad way.
So I decided to move to singleton pattern.
I need someone to review this code and confirm me that I made a good database access via singleton pattern or I am doing something wrong:
This is the class:
#interface DataAccessController : NSObject{
sqlite3 *databaseHandle;
}
+ (id)sharedManager;
-(void)initDatabase;
...
+ (id)sharedManager {
static DataAccessController *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (id)init {
if (self = [super init]) {
[self initDatabase]; // open database connection
}
return self;
}
...
AppDelegate.m
DataAccessController *d = [DataAccessController sharedManager];
Usage through the application every time I need data I use:
DataAccessController *d = [DataAccessController sharedManager];
NSMutableArray* data = [d getAllRecordedUnits];
The problem with database access is not whether you use a singleton pattern or not, but whether you access the database always from the main thread or also from other threads.
In fact, in most situations I use a singleton for database access (albeit by keeping the database connection in my app delegate). But you should make sure you always access the database on the main thread (which might be not performing well if you access it a lot and from different threads), or keep different contexts for different threads, synching them where needed.
Have a look here for an explanation:
Apple Core Data Documentation
Apple Core Data Performance Considerations
Apple Core Data Concurrency
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am building an app where the value of an integer can change randomly and I want to be able to hide or show a UIButton depending on the value of this integer.
However, I don't know how to trigger the status change when the value of the integer changes.
Is there any easy way to do it on iOS?
Many thanks for your help
Use observer pattern for this
[self.myVC addObserver:self forKeyPath:#"IntegerProperty" options:0 context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self.myVC && [keyPath isEqualToString:#"IntegerProperty"]) {
//do work
}
}
Your integer should be a property of a class and you should access it properly via the provided accessors (so use self.xxx). Then you can use KVO.
you can use key value observer concepts, it works similar to notification centre...