English 中文(简体)
在将日期从一种格式改为另一种格式时产生无效价值。
原标题:null value generates while converting the date from one format to another format

I am having a date format "16-11-2011". but I want to convert it into "16 Nov" I written the code for it but null value generates instead of the format "16 Nov"

我已写了以下法典:

NSDate *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:@"CHECK_1"];
    NSLog(@"Date: %@",checkIn);

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"dd MMM"];
    NSString *checkInDate = [formatter stringFromDate:checkIn];
    NSLog(@"checkInDate:%@",checkInDate);

ns statements

Date: 16-11-2011
checkInDate:(null)

因此,我无法将其转化为我想要采用的形式。

plz帮助我解决问题。

最佳回答

I suspect that checkIn is a NSString not NSDate. Therefore, you would have to convert it to a NSDate first and then convert it back to the desired string format.

NSString *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:@"CHECK_1"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];

NSDate *checkInTempDate = [dateFormatter dateFromString:checkIn];

[dateFormatter setDateFormat:@"dd MMM"];
NSString *checkInDate = [dateFormatter stringFromDate:checkInTempDate];
问题回答

问题是第一点:

NSDate *checkIn = [[NSUserDefaults standardUserDefaults]valueForKey:@"CHECK_1"];

<代码>NSUserDefaults没有一种方法<>ForKey,即“关键价值编码”正在回升。 <0>NSUserDefaults也不适用日期。 您从<条码>NSUserDefaults中接过,通过一个附件<代码>NSDate,接受。

最后4行的法典是正确的,但需要日期、检查。 这是一种扼杀。

在第一行之后测试这一行的法典,无视任何不兼容的类型警告:

NSLog(@"class name: %s", class_getName([checkIn class]));




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签