Skip to content

Instantly share code, notes, and snippets.

View identity2's full-sized avatar
🍇
grapes

Ramen identity2

🍇
grapes
View GitHub Profile
@identity2
identity2 / GSoC17_YuChao_PowerUp_iOS.md
Last active August 23, 2017 16:59
Yu Chao - GSoC 2017 - Systers/PowerUp-iOS
@identity2
identity2 / DatabaseAccessor.swift
Created June 8, 2019 05:10
Database Accessor
class DatabaseAccessor {
// The instance of DatabaseAccessor
static let sharedInstance = DatabaseAccessor()
// Private initializer to avoid instancing by other class.
private init() {}
/* ... */
}
@identity2
identity2 / DatabaseAccessor.swift
Created June 8, 2019 05:11
Database Accessor 2
class DatabaseAccessor {
static let sharedInstance = DatabaseAccessor()
private init() {}
// The instance of the SQLite database
private var mainDB: FMDatabase?
public func initializeDatabase() {
let databasePath = (dirPath as NSString).appendingPathComponent("database.sqlite") as NSString
CREATE TABLE `Contact` (
`ID` INTEGER NOT NULL UNIQUE,
`Name` VARCHAR NOT NULL,
`Phone` INTEGER NOT NULL UNIQUE,
`City` VARCHAR NOT NULL,
PRIMARY KEY(`ID`)
);
struct Contact {
var id: Int
var name: String
var phoneNumber: Int
var city: String
}
func addContact(_ contact: Contact) {
let queryString = "INSERT INTO Contact (ID, Name, Phone, City) VALUES (\(contact.id), \(contact.name), \(contact.phoneNumber), \(contact.city))"
mainDB!.executeUpdate(queryString, withArgumentsIn: nil)
}
func updateContact(_ contact: Contact) {
let queryString = "UPDATE Contact SET Name = \(Contact.name), Phone = \(Contact.phoneNumber), City = \(Contact.city) WHERE ID = \(Contact.id)"
mainDB!.executeUpdate(queryString, withArgumentsIn: nil)
}
@identity2
identity2 / searchContactByID.swift
Created June 8, 2019 05:16
search contact by id
func searchContactByID(_ id: Int) -> Contact? {
let queryString = "SELECT * FROM Contact WHERE ID = \(id)"
// Execute query and save the results.
let queryResults: FMResultSet? = mainDB?.executeQuery(queryString, withArgumentsIn: nil)
// Check if there are results.
if queryResults?.next() == true {
let contactName = queryResults!.string(forColumn: "Name")
let contactPhone = Int(queryResults!.int(forColumn: "Phone"))
let moveAction = SKAction.move(to: CGPoint(x: 10.0, y: 20.0), duration: 5.0)
spriteNode.run(moveAction)
let scaleAction = SKAction.scale(to: 2.0, duration: 3.0)
spriteNode.run(scaleAction, completion: { spriteNode.color = SKColor.red })