在下面的图片中,您可以看到我编写的代码和所有变量的值:
class fun getCurrentShortDate() -> String {
var todaysDate = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var DateInFormat = dateFormatter.stringFromDate(todaysDate)
return DateInFormat
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的那样,当前日期没有问题,但是当我尝试将NSDate更改为字符串时,它就不会这样做了.
我只是有可能有一个对象数组MyObject,并且MyObject得到一个被调用的变量isTrue,除了循环整个数组以检查该数组中的所有对象是否为真,是否有任何短手这样做?谢谢.
我刚刚转换了我的小应用程序,但我发现了这个错误:'substring(from :)'已被弃用:请使用字符串切片下标和'partial range from'运算符
我的代码是:
let dateObj = dateFormatterFrom.date(from: dateStringa)
if dateObj != nil {
cell.detailTextLabel?.text = dateFormatterTo.string(from:(dateObj!))
} else {
let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
cell.detailTextLabel?.text = thisRecord.pubDate.substring(from: index)
}
Run Code Online (Sandbox Code Playgroud) 我已定义enum如下:
enum Type: String, Codable {
case text = "text"
case image = "image"
case document = "document"
case profile = "profile"
case sign = "sign"
case inputDate = "input_date"
case inputText = "input_text"
case inputNumber = "input_number"
case inputOption = "input_option"
case unknown
}
Run Code Online (Sandbox Code Playgroud)
映射JSON字符串属性.自动序列化和反序列化工作正常,但我发现如果遇到不同的字符串,反序列化将失败.
是否可以定义一个unknown映射任何其他可用案例的案例?
这可能非常有用,因为这些数据来自RESTFul API,可能会在将来发生变化.
如何nsuserdefault在swift中自动清除值?我已经尝试了这个,但它对我不起作用......
[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
Run Code Online (Sandbox Code Playgroud) 我想以这种格式显示日历

给用户.一种选择是使用"字符串范围"来获取各个日历组件.第二个是使用NSCalendar来获取它对我来说看起来更好(是吗?).所以我的代码如下.但是有两个问题.
任何人都知道如何得到我需要的东西?附加图像是我想要实现的目标.在那里,我使用三个UILabel用于"日期",第二个用于"月,年",第三个用于"时间".
任何帮助,将不胜感激.
var inputDateString = "Jun/12/2015 02:05 Am +05:00"
override func viewDidLoad() {
super.viewDidLoad()
let newDate = dateformatterDateString(inputDateString)
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: newDate!)
let hour = components.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day
println(newDate)
println(components)
println(day) // 12
println(month) // 6 -----> Want to have "Jun" here
println(year) // 2015
println(hour) // 2 …Run Code Online (Sandbox Code Playgroud) 我有一个字符串数组,
let array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
Run Code Online (Sandbox Code Playgroud)
我想将输出按升序排序为,
let sorted = [ "1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA" ]
Run Code Online (Sandbox Code Playgroud)
我尝试使用sorted命令,但遇到2位以上时不起作用,例如:100,101,200等.
array.sorted { $0? < $1? }
Run Code Online (Sandbox Code Playgroud)
得到这个的简单方法是什么?
我正在尝试根据 JSON 响应创建视图模型,但出现以下错误。
import Foundation
import SwiftUI
public class DeclarationViewModel: ObservableObject {
@Published var description: [DeclarationListViewModel]?
init() {
self.description = [DeclarationListViewModel]()
}
init(shortDescription: [DeclarationListViewModel]?) {
self.description = shortDescription
}
}
public class DeclarationListViewModel: ObservableObject, Hashable {
@Published var yesNo: Bool?
@Published var title: String?
}
Run Code Online (Sandbox Code Playgroud)
尝试在 foreach 中使用结果
谢谢你的帮助。如果需要更多详细信息,请告诉我。
在CABasicAnimation.fromValue我想要转换CGPoint为"类"所以我使用NSValue valueWithPoint但在设备模式或模拟器一个不工作...需要使用NSMakePoint或CGPointMake如果在设备或模拟器.