How to convert this NSTimeInterval line from Swift to Objective-C - objective-c

How to convert this into Objective-C ?
let date = NSDate(timeIntervalSinceReferenceDate: interval)
I tried this:
NSDate *date = [NSDate timeIntervalSinceReferenceDate: (interval)];
It generates an error of course.
Tells me: No known class method for selector timeIntervalSinceReferenceDate:
Also here is some context:
NSTimeInterval interval = [[NSDate alloc]init].timeIntervalSinceReferenceDate + [NSDate weekInSeconds].doubleValue;
NSDate *date = [NSDate timeIntervalSinceReferenceDate: (interval)];
return [self isSameWeekAsDate:(date)];

timeIntervalSinceReferenceDate: is not a method implemented on the NSDate class. When bridging from Objective-C to Swift, Swift does some renaming with initializers.
In order to initialize an NSDate object in Objective-C with a reference date, you must call either the class method dateWith... or the instance method initWith...:
NSDate * const date = [NSDate dateWithTimeIntervalSinceReferenceDate: timeInterval];
or...
NSDate * const date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: timeInterval];
I've added the const here to make this more closely match the Swift code with the let declaration.

Related

How to initialise NSDate in a parameterised constructor in Objective C?

This is the code written in Objective C where I am initialising values into a parameterised constructor. While all the fields are taking entries properly, I am having trouble entering in the NSDate field. Given below is the Constructor declaration, implementation and finally the method call.
//Init declaration
-(instancetype)initWithParam1:(NSNumber*)customerId_ withParam2: (NSString*)firstName_ withParam3:(NSString*)lastName_ withParam4:(NSDate*)dateOfBirth_ withParam5:(NSString*)address_ withParam6:(NSNumber*)mobileNumber_;
//Init definition
-(instancetype)initWithParam1:(NSNumber *)customerId_ withParam2:(NSString *)firstName_ withParam3:(NSString *)lastName_ withParam4:(NSDate *)dateOfBirth_ withParam5:(NSString *)address_ withParam6:(NSNumber *)mobileNumber_
{
self = [super init];
if(self)
{
self.customerId = customerId_;
self.firstName = firstName_;
self.lastName = lastName_;
self.dateOfBirth = dateOfBirth_;
self.address = address_;
self.mobileNumber = mobileNumber_;
}
return self;
//Init call
Customer *c1 = [[Customer alloc]initWithParam1:#1001 withParam2:#"Aman" withParam3:#"Zaidi" withParam4:#"22-05-1993" withParam5:#"Bangalore" withParam6:#9567812345];
Any suggestions on how to enter date in that field. Sorry for the naivety but I am totally new to Objective C and I am getting used to the syntaxes.
The date is a string.
You need to change the parameter to NSString
... withParam4:(NSString *)dateOfBirth_ ...
and in the init method convert the string to NSDate with NSDateFormatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"dd-MM-yyyy";
self.dateOfBirth = [formatter dateFromString:dateOfBirth_];

Why can't I store timeIntervalSince1970 return value to a pointer?

I'm a beginner (at programming) learning Objective-C. Xcode will not let me allocate a return value from an NSDate object; I am confused by this.
NSDate *now = [NSDate date];
long seconds = [now timeIntervalSince1970];
If I use a pointer to store the seconds:
long *seconds = [now timeIntervalSince1970];
I see the error:
Initializing 'long *' with an expression of incompatible type 'NSTimeInterval' (aka 'double')
Why can't I say "Give me the return value of now.timeIntervalSince1970 and stick at this address"?
Another question is, why can't I initialize an object and use it without needing a pointer? This will not work.
NSDate dateObject = [NSDate date];
I realize my question probably has a very easy answer, but the book doesn't explain any of the "why does it work this way" questions.
It's because [NSDate timeIntervalSince1970] returns the value as an NSTimeInterval which is a typedefd double, which is a primitive type, not an object type.
The value counts the number of seconds since 1-Jan-1970, and does not need to be an object.
You could store it in a pointer, but that would be silly:
NSTimeInterval *elapsed = (NSTimeInterval *)malloc(sizeof(NSTimeInterval));
*elapsed = [now timeIntervalSince1970];
...
free(elapsed);
timeIntervalSince1970 returns an NSTimeInterval which is a type def of double. In the Apple documentation you will see this defined like
objective-c
typedef double NSTimeInterval;
Swift
typealias NSTimeInterval = Double
and double is a primitive and not an object type so shouldn't be a pointer (Shouldn't is in Apple documentation). The Apple documentation also states the below
NSTimeInterval is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.
Here is the Apple Documentation

Objective-C : Create and using Objects [duplicate]

This question already has answers here:
What is the difference between class and instance methods?
(18 answers)
Closed 9 years ago.
I'm a c++ & c programmer , and i'm new to the world of objective-C , so i have some problem
understanding how it works , here a short code , that confused me,
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
#autoreleasepool {
NSDate *now = [NSDate date];
NSLog(#"The date is %#", now);
double seconds = [now timeIntervalSince1970];
NSLog(#"It has been %f seconds since the start of 1970.", seconds);
}
return 0; }
now is pointer to an object type NSdate my question is why we can not do this :
double seconds = [NSDate timeIntervalSince1970];
normally the first part is the type of the object and the second part is the method
i'm sorry if this is a bad question but i want to understand Objective-C very well from the begining.
Thanks
You can do something similar with
[NSDate timeIntervalSinceReferenceDate];
Although the reference date in this case is 1 January 2001
But, this is a class method. You can call it on the class.
The other methods, such as timeIntervalSince1970 are instance methods, which need to be called on actual objects of the class. In NSDate's case there is no class method for the time interval since 1970.
If you really want to, you can add a Category on to NSDate and add a class method that does this.
This is a class method:
NSDate *now = [NSDate date];
You don't need an instance of the object.
This is an instance method:
[now timeIntervalSince1970];
And you need and instance of the object.
Same in C++ as: Class::classMethod() and myClass::instanceMethod()
More info here:
https://softwareengineering.stackexchange.com/questions/191856/what-is-a-static-method-compared-to-instance-class-private-public-methods

How to set current date on NSDatePicker object in osx

I am new to OSX App development. In one of the sample menubar Apps I did, I used an NSDatePicker object. But it is not displaying the current date. How can i display the current date using an NSDatePicker object.
Did you try
NSdate *currentDate = [NSDate date];
datePicker.date = currentDate;
??
this is the same as
NSdate *currentDate = [NSDate date];
[datePicker setDate:currentDate]
The main difference is that if currentDate is not equal to nil in your code, the datePicker's date does not get set!!
set current system time to Datepicker
#IBOutlet weak var txtStartTime: NSDatePicker!
swift
let now = Date()
txtStartTime.dateValue = now
As of OSX 10.4, the way to do this is
NSDate *currentDate = [NSDate date];
[datePicker setDateValue:currentDate];
You can set date to date picker in Swift 3.2like,
self.datePicker.dateValue = NSDate() as Date

Create NSDate from Unix timestamp

How do I create an NSDate from a Unix timestamp?
channel.startDate = [NSDate dateWithTimeIntervalSince1970:
(NSTimeInterval)[channelJson objectForKey:#"broadcastStartedTime"]];
I get this error:
104: error: pointer value used where a
floating point value was expected
channels.startDate is an NSDate*. The value for the key "broadcastStartedTime" is a Javascript Number converted into an NSNumber or NSDecimalNumber by the SBJson parser library.
Try this instead:
NSNumber *startTime = channelJson[#"broadcastStartedTime"];
channel.startDate = [NSDate dateWithTimeIntervalSince1970:[startTime doubleValue]];
Your value is trapped in a pointer type of NSNumber. The dateWithTimeIntervalSince1970 method is expecting a primitive NSTimeInterval (which, under the covers, is a double).
Use -doubleValue:
// NSTimeInterval is just a typedef for double
NSTimeInterval interval = [[channelJson objectForKey:#"broadcastStartedTime"] doubleValue];
channel.startDate = [NSDate dateWithTimeIntervalSince1970:interval];
You need to unwrap the NSNumber:
channel.startDate = [NSDate dateWithTimeIntervalSince1970:[[channelJson objectForKey:#"broadcastStartedTime"] doubleValue]];
NSTimeInterval (which is unix timestmap) to NSDate conversion in Swift:
let timeInterval = NSDate().timeIntervalSince1970 // the the unix timestamp
NSDate(timeIntervalSince1970: timeInterval)