我创建了一个使用sqlite DB的应用程序.我有一个问题,它是任何时候运行我的应用程序在我的表sqlite中添加NSDictionary.我想要一次检查sqlite,如果表sqlite中存在数据,则不添加其他添加它.
我的代码:
ViewController.h
#import <UIKit/UIKit.h>
#import "sqlite3.h"
@interface ViewController : UIViewController
{
sqlite3 * database;
}
@end
Run Code Online (Sandbox Code Playgroud)
ViewController.m
#import "ViewController.h"
#define DatabaseName @"data.sqlite"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *idd = [NSArray arrayWithObjects:@"122",@"234",@"453", nil];
NSArray *name = [NSArray arrayWithObjects:@"janatan",@"fred",@"john", nil];
NSArray *age = [NSArray arrayWithObjects:@"23",@"35",@"12", nil];
NSArray *sex = [NSArray arrayWithObjects:@"male",@"male",@"male", nil];
for (int i = 0; i < [idd count]; i++)
{
NSString * a = [idd objectAtIndex:i];
NSString * b = [name objectAtIndex:i];
NSString * …Run Code Online (Sandbox Code Playgroud) 我正在尝试UITableView在Swift 中创建一个。我遵循了一个教程,但是table没有我尝试将其放入代码中的值。这是代码:
class settingsVC2: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
}
}
Run Code Online (Sandbox Code Playgroud) 我有三个UIButton,我以编程方式创建了约束,在某些情况下,我删除其中一个UIButton按钮button.removeFromSuperview(),其余两个按钮将根据优先级设置约束。问题是当我删除一个UIButton(buttonWink)时,从 viewLifeCycle 开始viewWillLayoutSubviews,应用程序将在下面一行崩溃
buttonWink.translatesAutoresizingMaskIntoConstraints = false
Run Code Online (Sandbox Code Playgroud)
当然,因为 ButtonWink 已从超级视图中删除,但是我们可以在设置约束之前进行检查:
if buttonWink != nil {
buttonWink.translatesAutoresizingMaskIntoConstraints = false
}
Run Code Online (Sandbox Code Playgroud)
但是通过对每个按钮检查 nil 会使代码变得冗长,有什么方法可以做到这一点吗?我会非常感谢朋友们。
输出 -
我在这里附上我尝试过的完整代码。
import UIKit
class MasterViewController: UIViewController {
@IBOutlet weak var buttonMessage : UIButton!
@IBOutlet weak var buttonLike : UIButton!
@IBOutlet weak var buttonWink : UIButton!
@IBAction func tapsOnLike(_ sender: UIButton) {
sender.removeFromSuperview()
}
@IBAction func tapsOnWink(_ sender: UIButton) {
sender.removeFromSuperview()
}
@IBAction func tapsOnNextButton(){
let vc = …Run Code Online (Sandbox Code Playgroud) 下面是使用示例,NSSortDescriptor它给出的结果与Swift 中sortedByFirstNameSwifty需要什么相同 ?NSSortDescriptor
class Person: NSObject {
let firstName: String
let lastName: String
let age: Int
init(firstName: String, lastName: String, age: Int) {
self.firstName = firstName
self.lastName = lastName
self.age = age
}
override var description: String {
return "\(firstName) \(lastName)"
}
}
let a = Person(firstName: "a", lastName: "b", age: 24)
let b = Person(firstName: "c", lastName: "d", age: 27)
let c = Person(firstName: "e", lastName: "f", age: 33)
let d = Person(firstName: "g", …Run Code Online (Sandbox Code Playgroud)