我正在寻找一种方法来自动序列化和反序列化Swift中的类实例.我们假设我们定义了以下类......
class Person {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}
Run Code Online (Sandbox Code Playgroud)
......和Person实例:
let person = Person(firstName: "John", lastName: "Doe")
Run Code Online (Sandbox Code Playgroud)
JSON表示person将如下:
{
"firstName": "John",
"lastName": "Doe"
}
Run Code Online (Sandbox Code Playgroud)
现在,这是我的问题:
person实例并获取上述JSON,而无需手动将类的所有属性添加到转换为JSON的字典中?Person?同样,我不想手动映射属性.以下是使用Json.NET在C#中执行此操作的方法:
var person = new Person("John", "Doe");
string json = JsonConvert.SerializeObject(person);
// {"firstName":"John","lastName":"Doe"}
Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json);
Run Code Online (Sandbox Code Playgroud) 我怎么能允许我在iOS 8.3 SDK上使用Swift编写的通用应用程序仅支持iPhone上的纵向模式,而是支持iPad上的纵向和横向模式?
我知道在过去这已经在AppDelegate中完成了.我怎么能在Swift中这样做?
我有一个UIScrollView的子类,我需要在内部响应滚动行为.但是,viewcontroller仍然需要监听滚动的委托回调,所以我不能直接窃取我的组件中的委托.
有没有办法保持名为"委托"的属性,只是听从它发送的消息,或者以某种方式内部劫持委托属性并在运行一些代码后向外转发消息?
如何确定通用结构的两个实例是否属于同一类型?
例如,给定以下结构:
struct FooBar<T> {
let variable: T
init(arg: T) {
variable = arg
}
}
Run Code Online (Sandbox Code Playgroud)
以下片段:
let foo = FooBar(1)
let bar = FooBar(1.0)
let baz = FooBar("1")
Run Code Online (Sandbox Code Playgroud)
我如何确定foo,是bar,或是baz相同或不同的类型?
func areExactType(x: FooBar) -> Bool {
return self.dynamicType === x.dynamicType
}
Run Code Online (Sandbox Code Playgroud)
这给了
类型'Foo'不符合协议'AnyObject'
func areExactType(x: FooBar) -> Bool {
return self.dynamicType === x.dynamicType
}
Run Code Online (Sandbox Code Playgroud)
这给了
无法使用类型'(Foo.Type,Foo.Type)'的参数列表调用'=='
func areExactType(x: FooBar) -> Bool {
return self is x.dynamicType
}
Run Code Online (Sandbox Code Playgroud)
这给出了三个错误:
一行上的连续陈述必须用';'分隔
(这要在句点和'dynamicType'之间加一个分号)
虚线类型的预期标识符
和
期待的表达
我使用"返回键"的"下一步"值来获取"下一步"按钮代替"完成"按钮,但是(显然)按下它不会自动移动到我视图中的下一个UITextField.
这样做的正确方法是什么?我见过很多答案,但是任何人都有快速解决方案吗?
所以我有一个这样的prepareForSegue方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromEventTableToAddEvent" {
let addEventViewController: AddEventViewController = segue.destinationViewController as! AddEventViewController
addEventViewController.newTagArray = newTagArray
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在运行时,应用程序崩溃并记录此消息:
无法转换'UINavigationController'类型的值
之所以出现错误,是因为addEventController嵌入在导航控制器中,但我不知道如何设置segue以便将destinationViewController设置为NavigationController,还允许我将prepareForSegue方法中的变量传递给addEventController
那我怎么用Swift呢?
我非常感谢您提供的帮助和时间作为回应.它有很多帮助
我有两个数组Data1和Data2,我希望将这些数据中的数据(它们包含字符串)填充到两个不同部分的tableview中.第一部分应标题为"Some Data 1",第二部分标题为"KickAss".
我有两个部分填充第一个数组的数据(但没有标题).
到目前为止,这是我的代码:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowCount = 0
if section == 0 {
rowCount = Data1.count
}
if section == 1 {
rowCount = Data2.count
}
return rowCount
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let ip = indexPath
cell.textLabel?.text = Data1[ip.row] as String
return cell
} …Run Code Online (Sandbox Code Playgroud) 我经常遇到这个问题,我希望我的观点以一种方式排列为纵向,而对于景观则有一种截然不同的方式.
有关简化示例,请考虑以下事项:


请注意,在纵向上图像是垂直堆叠的,但在横向上,它们是并排的?
当然,我可以手动计算位置和大小,但对于更复杂的视图,这很快就会变得复杂.另外,我更喜欢使用Autolayout,因此设备特定的代码更少(如果有的话).但这似乎要写很多 Autolayout代码.有没有办法通过故事板为这类东西设置约束?
使用表达式成员允许您将方法或属性的主体定义为没有return关键字的单个表达式(如果它返回了某些内容).
例如,它变成了这些
int Method1()
{
return 5;
}
void Method2()
{
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)
进入这些
int Method1() => 5;
void Method2() => Console.WriteLine();
Run Code Online (Sandbox Code Playgroud)
当您从正文中抛出异常时,会产生差异:
void Method3()
{
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
但是,以下内容将无法编译:
void Method3() => throw new Exception();
Run Code Online (Sandbox Code Playgroud)
以下消息:
Warning The member 'Program.Exception()' does not hide an inherited member. The new keyword is not required.
Error 'Program.Exception()' must declare a body because it is not marked abstract, extern, or partial
Error ; expected
Error Invalid token 'throw' in class, struct, or …Run Code Online (Sandbox Code Playgroud) 是否有Swift相当于
__attribute((objc_requires_super))
Run Code Online (Sandbox Code Playgroud)
如果方法没有调用它的超级方法,它会发出警告?
基本上,如果重写的方法不调用其超级方法,我想警告(或甚至更好地抛出编译器错误).
swift ×7
ios ×5
arrays ×1
autolayout ×1
c# ×1
c#-6.0 ×1
generics ×1
ipad ×1
json ×1
objective-c ×1
roslyn ×1
sections ×1
segue ×1
struct ×1
types ×1
uikit ×1
uitableview ×1
uitextfield ×1