Getting error when trying to pass integer to method - iOS [closed] - ios

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.

Related

Call metatable methods inside metatable itself [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
Is there a way to call metatable methods inside the metatable itself? For example
local t = {}
local mt = {
__index = {
dog = function() print("bark") end,
sound = function() t:dog() end
}
}
setmetatable(t,mt)
t:Sound()
raises this error:
attempt to call method 'Sound' (a nil value)
because you don't have Sound. Only sound.

NSLog not printing out BOOL result to console [closed]

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;
}

How do i keep the label updated as the time passes in ios [closed]

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
How do i keep the UILabel updated as the time passes when a post is posted in ios ..like if >its posted just now it should say 0s and as time passes it changes to >1min,2min...1hr,2hr...1day and so in ios
I am new to the ios development
Thanks for any help!
You need to use NSTimer for this purpose if I understand your questions properly.
Doing something on a regular interval is pretty easy in iOS.
In your main code:
[NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: #selector(doSomething:)
userInfo: nil
repeats: YES];
Then add the doSomething method:
- (void)doSomething:(NSTimer *)timer {
NSLog(#"We did it!");
}
First of all you need to keep the date when user posted. You can easily do that by using:
NSDate* postedDate = [NSDate now];
Than if you want to find a seconds left from that time you can use:
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:postedDate];
This gives you seconds from posting date - you need to format output.
For keeping label updating itself you should use NSTimer:
[NSTimer scheduledTimerWithTimeInterval: 1.0f
target: self
selector: #selector(updateLabel:)
userInfo: nil
repeats: YES];
Where updateLabel will format "timeDiff" into something human readable like 1minute / day etc...

Save button's new state when exiting the app [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to have a button in my app which gets hidden after the first time it's tapped.
So what should I do to save it's hidden property after terminating the app?
And the second thing to ask is almost the same:
How can I change the frame of it after terminating the app?
Thanks in advance
Would probably recommend saving a value in NSUserDefaults Maybe a BOOL for Hidden and check it on start up. An example with a bool would be:
set:
[[NSUserDefaults standardUserDefaults] setBool: YES forKey #"someKey"];
get:
[[NSUserDefaults standardUserDefaults] objectForKey:#"someKey"];
could do the same for any object using:
[[NSUserDefaults standardUserDefaults] setObject: id forKey #"someKey"];

Call a method after 5 sec. without NSTimer

I want to call any method OR event after 5 second in iphone without using NStimer.
Use performSelector:withObject:afterDelay: method.
[self performSelector:#selector(methodName) withObject:nil afterDelay:5];
Or use GCD:
double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
/* code to be executed on the main queue after delay */
});
The question sounds like a very pointless assignment. That calls for an equally useless answer:
sleep(5);
[self myMethodToCallAfter5Seconds];
Edit: Since people don't seem to like my answer, here are some thoughts:
The OP asked for a way to send a message after a delay. He explicitly asked for a way to do this without using an NSTimer yet he didn't specify why. As there are many ways to send a message after a delay, picking one depends upon the reason why to avoid any specific method. Not knowing the reason for refusing an obvious approach there's only speculation.
One possible situation could be a background thread without a runloop that should await the delay. Here my (not totally serious) proposal could even be a proper solution when blocking the thread is not an issue. In this scenario neither NSTimer nor performSelector:withObject:afterDelay: could be used.
I added this answer to highlight the fact that the question makes little sense without giving a reason. As my answer fulfills the specification it shows the pointlessness of the question.

Resources