将字符串转换为时间戳并在swift 2.0中显示标签值

时间:2017-04-27 11:23:09

标签: swift2

我有一个用Objective c编写的代码,那么如何在swift中使用这段代码

NSString *dateString = [dic2 objectForKey:@"time"];
NSTimeInterval interval = [dateString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"dd MMM, YYYY"];
NSString *datenewString = [format stringFromDate:date];
cellNotification.lbldatenotificationN.text = [NSString stringWithFormat:@"%@", datenewString];

任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码。

Swift 2.3

new Vue({
  el: '#app',
  data: {
    skin: ''
  },
  methods: {
    setSkin: function() {
      this.skin = ...;
    },
    saveSkin: function() { // call this function when you need to save skin data into localStorage
      window.localStorage.setItem("skin", this.skin);
      ....
    }
  },
  mounted() {
    this.setSkin(); // retrieve skin data from localStorage, etc.
  }
});

swift 3.1

var dateString = (dic2["time"] as! String)
var interval = dateString.doubleValue
var date = NSDate(timeIntervalSince1970: interval)
var format = NSDateFormatter()
format.dateFormat = "dd MMM, YYYY"
var datenewString = format.stringFromDate(date)
cellNotification.lbldatenotificationN.text = "\(datenewString)"