2019-01-01から1年間の記事一覧

Data型とString型の互換

String -> Data // その結果はData?型。 let data = "aaa".data(using: .utf8) Data -> String let str = String(data: someData, encoding: .utf8)

ObservableObjectをCodableさせる

class Person: ObservableObject, Codable { @Published var name = "" @Published var age = 0 enum CodingKeys: CodingKey { case name, age } init() { } required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy:…

SwiftUIにCore Dataの使用 データ新規、変更と削除

管理用オブジェクト取得 まずはContentViewに@Environment(\.managedObjectContext) var contextを定義する。このcontextはCore Dataの管理用のオブジェクト。 context定義だけで使用できる原因は、プロジェクト作成時"Use Core Data"をチェックする時、Scen…

SwiftUIにCore Dataの使用 データ取得

Core Data導入 Core Dataを使うため、プロジェクト作成時「Use Core Data」をチェックする。 プロジェクト作成後、拡張子「.xcdatamodeld」が自動作成される。その中にEntityを作成する。 データの取得 @FetchRequest( entity: Singer.entity(), sortDescrip…

SwiftUI画面遷移の二つ方法

方法一:sheet struct ContentView: View { @State private var showSecondView = false var body: some View { Button("Show Second View") { self.showSecondView.toggle() } .sheet(isPresented: $showSecondView) { SecondView() } } } struct SecondVie…

SwiftUIよく使う部品のサンプル

TextField TextField("Your answer is ...", text: $answer) .keyboardType(.numberPad) .textFieldStyle(RoundedBorderTextFieldStyle()) Stepper Stepper(value: $sleepAmount, in: 4...12, step: 0.25) { Text("\(sleepAmount, specifier: "%g") hours") …

CatalystでMacOS Appをリリースしてみた

先日私のアプリ「Pro Table Tennis Scoreboard」はMac App Storeにリリースしました。 本文はiOSアプリは既に存在している状態で、Mac App Storeにリリースするためのステップを紹介します。 プロジェクト画面で「Mac」をチェックすること。 なぜか、MacOS A…

画像resize実用的なウェブサイト

https://resizeimage.net

Realm 既にリリースされたアプリのテーブル項目変更対応

例:テーブルHistoryDetailに項目isCheckOut(Bool)を削除、項目checkOutDarts(Int)を追加して、以前isCheckOutはtureの場合、checkOutDartsは3に設定、それ以外0に設定: appDelegate.swiftのfunctionapplication(_ application: UIApplication, didFinishLa…

Open URL by Safari

if let url = URL(string: "https://cecilma2018.blogspot.com") { UIApplication.shared.open(url, options: [:]) }

Timer

var timer: Timer! timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (_) in self.perform() }) func perform() { // some process if XXX { // time to end the Timer timer.invalidate() } }

接触検知

import SpriteKit class MyScene: SKScene, SKPhysicsContactDelegate { override func didMove(to view: SKView) { ... physicsWorld.contactDelegate = self } func didBegin(_ contact: SKPhysicsContact) { guard let name1 = contact.bodyA.node?.name …

Sceneの遷移

if let nextScene = SKScene(fileNamed: "GameScene") { nextScene.scaleMode = scaleMode let transition = SKTransition.flipVertical(withDuration: 0.5) view?.presentScene(nextScene, transition: transition) }

Alertを表示する

let alertController = UIAlertController(title: "Title", message: "Some message", preferredStyle: .alert) let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(alertAction) view?.window?.rootV…

それぞれサイズのAppアイコン作成

まず自分で1024*1024のアイコン画像を画く。 https://appiconmaker.coで各サイズのアイコンが作成できる。

2倍解像度の画像の追加

画像ファイル名を"xxx@2x.png"のようにリネームして、XcodeのAssets.xcassetsにドロップする。

動的なプロパティ名でプロパティを読み取り

src配下(Mavenの場合src.main.resources配下)app.propertiesの内容: weapon.price.1=1000 weapon.price.2=2500 前回紹介された「@Value("${プロパティ名}")」の方法では、プロパティ名は動的に変更できませんので、新たな方法が必要です。 Java Configよ…

プロパティの読み取り

プロパティファイル作成 src配下(Mavenの場合src.main.resources配下)でapp.propertiesファイル作成する。 文字化け防止のため、EclipseのLimyプロパティエディターで編集する。 記入例:list.max=1000 Java Configより定義 ... @PropertySource("classpat…

メッセージ出力

Java Config記法 @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasenames("messages"); return messageSource; } xml記法

Spring MVCを使うために最低限の配置

Java Config記法 【前提】各ファイル格納パッケージ(パッケージ名は自由): コンフィグファイル:com.mxy.config コントローラーファイル:com.mxy.controller サービスファイル:com.mxy.service jspファイル:/WEB-INF/jsp/ web.xml <web-app ...> ... <servlet> <servlet-name>app</servlet-name> <servlet-class> org.spri</servlet-class></servlet></web-app>…

簡単なAlert機能

extension UIViewController { func alert(title: String, message: String) { let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) let alertAction = UIAlertAction(title: "OK", style: .default, handler…

特定頻度でユーザのレビューを要求する

import StoreKit func askUserToRate() { var gamePlayed = UserDefaults.standard.integer(forKey: "gamePlayed") gamePlayed += 1 UserDefaults.standard.set(gamePlayed, forKey: "gamePlayed") if gamePlayed % 10 == 0 { SKStoreReviewController.reque…