当NSDate为nil

时间:2015-11-20 14:28:00

标签: ios objective-c nsstring nsdate nsdateformatter

我使用日期格式化程序来获取新字符串,但在某些情况下,startDate可以是nil。

resultDate = [NSString stringWithFormat:@"%@", [formatter stringFromDate:startDate]];

然后resultDate得到(null)字符串(NSTaggedPointerString),但是如果格式化程序将nil作为日期参数,我希望得到nil。

2 个答案:

答案 0 :(得分:2)

目前接受的答案实际上并不正确。

当您将stringFromDate日期传递给nil时,

nil会返回[NSString stringWithFormat:"%@", nil]

你的字符串是“(null)”的原因是因为rdd = sc.parallelize([('a', 1), ('a', 2), ('b', 1), ('b', 3), ('c',1), ('ef',5)]) rdd1 = rdd.repartition(4) rdd2 = rdd.partitionBy(4) rdd1.glom().collect() [[('b', 1), ('ef', 5)], [], [], [('a', 1), ('a', 2), ('b', 3), ('c', 1)]] rdd2.glom().collect() [[('a', 1), ('a', 2)], [], [('c', 1)], [('b', 1), ('b', 3), ('ef', 5)]] 返回字符串“(null)”

答案 1 :(得分:1)

NSDateFormatter上的(nonnull NSDate *)实例方法需要NSString * _Nonnull并返回_Nonnull

stringFromDate关键字指定NSString *始终返回stringFromDate且永不返回nil,因此在零日期"null"将返回字符串/* Normal validate initialisation. */ $('#myForm').validate({ /* Use the submitHandler method to add custom-selector-based validation. */ submitHandler: function(form, ev) { /* Find your dynamic field/s. Note that you may want to access them via a scope external to validate, as any selection you do in this internal scope will be static from the form's pre-edit state. */ var el = $('#selector'); /* Do your custom validation. */ if ( el.val() !== 'A' ) { /* Show any errors:- 'fieldname': 'error message'. */ this.showErrors({ 'name-of-a-field-near-where-you-want-your-error-placed': 'Please enter "A" to continue' }); /* Prevent form submission. */ return; } } });