从字典响应中更改NSUserDefaults standardUserDefaults的日期值

时间:2017-12-06 00:15:33

标签: objective-c datetime nsuserdefaults

我的方法收到名为responseDict的词典,其中包含密钥,例如idstatusnamephoneNumberdate等在

responseDict[@"data"][@"value"][n] 

n代表)

如果我们的代码使用NSUserDefaults来访问数据:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

如何将date MySQL属性2017-05-12 10:00:00更改为Dec 5, 2017 10 AM

我尝试了以下操作,但似乎没有更改defaults中的UILabel

[defaults setInteger:[dictionary[@"id"] integerValue] forKey:@"ID"];
[defaults setObject:dictionary[@"status"] forKey:kNSUDoctorStatus];
[defaults setInteger:[dictionary[@"dur"] integerValue] forKey:@"duration"];
[defaults setObject:[NSString stringWithFormat:@"%@ some",dictionary[@"date"]] forKey:@"date"];
[defaults synchronize];

修改

的UILabel

UILabel似乎已添加到视图中:

UILabel *vb = (UILabel *)[self.view viewWithTag:personMessageViewTag];

由以下人员定义:

#define personMessageViewTag 3001

医生班

还初始化了PersonDetailView个类:

PersonDetailView *personOnTheView;

用方法调用:

personOnTheView = [PersonDetailView sharedInstance];
personOnTheView.delegate = self;
[personOnTheView updateValues];
[self.view addSubview:personOnTheView];

方式: 以下是[personOnTheView updateValues]

的代码
-(void)updateValues{

    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    NSString *strImageUrl = [NSString stringWithFormat:@"%@%@",baseUrlForXXHDPIImage,[ud objectForKey:kOnGoingBookingPersonProfileImageKey]];
    _doctorName.text = [ud objectForKey:kOnGoingBookingPersonNameKey];

    NSString *at = [NSString stringWithFormat:@"%@",flStrForObj([Helper getLocalDate:[ud objectForKey:kOnGoingBookingPersonBookingDate]])];

    _appoinmentTime.text = at;

     float rating = [[ud objectForKey:kOnGoingBookingPersonRatingKey] floatValue];

    _starRatingView.value = rating;

    [_profilepic sd_setImageWithURL:[NSURL URLWithString:[Helper removeWhiteSpaceFromURL:strImageUrl]]
                  placeholderImage:[UIImage imageNamed:@"doctor_image_thumbnail"]
                         completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
                         }];
}

问题:

每次我改变或修改:

[defaults setObject:dictionary[kPNPayloadAppoinmentTimeKey] forKey:kOnGoingBookingPersonBookingDate];

要:

[defaults setObject:[NSString stringWithFormat:@"%@ some",dictionary[@"date"]] forKey:@"date"];

或者就像:

一样简单
[defaults setObject:@"Something here" forKey:@"date"];

显示空白。

其他信息编辑:

[Helper getLocalDate]代码:

+(NSString *)getLocalDate:(NSString *)gmtdate {

    NSString *dateStr = gmtdate;
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *destinationDate = [dateFormatter1 dateFromString:dateStr];

    NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
    [dateFormatters setDateFormat:@"MMM d, yyyy HH a"];
    dateStr = [dateFormatters stringFromDate: destinationDate];
    // NSLog(@"DateString : %@", dateStr);
    return dateStr;
}

1 个答案:

答案 0 :(得分:0)

如果您在日期字符串

中有时区,则可以使用此方法获取本地格式的日期
-(NSDate*)convertDateToLocal:(NSString *)strDate
{
    NSDateFormatter *localFormat = [[NSDateFormatter alloc] init];
    [localFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
    NSDate *localDate = [localFormat dateFromString:strDate];
    return localDate;
}

如果您没有时区,则以下是两种日期转换的公开方法

1)来自字符串的日期,

+ (NSDate *)dateFromString:(NSString *)strSource inFormat:(NSString *)strTargetFormat
{
  NSDateFormatter *dtfFormatter = [[NSDateFormatter alloc] init];
  [dtfFormatter setDateFormat:strTargetFormat];

  return [dtfFormatter dateFromString:strSource];
}

2)日期字符串,

+ (NSString *)stringFromDate:(NSDate *)dtSourceDate inFormat:(NSString *)strTargetFormat
{
  NSDateFormatter *dtfFormatter = [[NSDateFormatter alloc] init];
  [dtfFormatter setDateFormat:strTargetFormat];

  if (dtSourceDate != nil)
    return [dtfFormatter stringFromDate:dtSourceDate];
  else
    return @"";
}

希望这有帮助。