[発生環境] Xcode 13.5 iOS 15.5 iPhone 13 Pro Simulator [解決したい問題] xxxが表示されない xxxが表示されるようにしたい [発生手順] 1. Aボタンをタップする 2. Bボタンをタップする [コード] コードを見せても問題ない人はリンクは貼ったり、記述したり 最小限で発生するコードだと尚望ましいswift testで走らせないテストの指定ってできないですか? Xcodeだとdisableできるので重宝するのですがswift package generate-xcodeproj して Xcode 上で実行はダメですか?swift testもやらないで良いわけじゃないですよね? パフォーマンステスト等機能に直結しないものは走らせたくないという状況です。swift testを極力しないという方針で回避が良いか。SWIFT_PACKAGEの存在自体知らなかったです。SWIFT_PACKAGE 定義が含まれているので、それを消した方が良いです。ex をもつプロトコロルを作成してそれに準拠させるという実装スタイルがあるNonError_1_Contents.swiftとNonError_2_Contents.swiftは associatedtype CompatibleType と associatedtype CompatibleTyp になっていますよ。Error_Contents.swiftと同じ状態になります。Error_Contents.swift において、 extension String: ACompatible { } から推測される String. associatedtype の型はAExample<Self>
extension String: BCompatible { }から推測される String. associatedtype の型はBExample<Self> で、型が一致していないのでエラーになりますね。 (edited)Playground execution failed: error: 20170520-221301.xcplaygroundpage:42:1: error: type 'String' does not conform to protocol 'BCompatible' extension String: BCompatible { } // ここで error: type 'String' does not conform to protocol 'BCompatible' が出る 20170520-221301.xcplaygroundpage:24:9: note: protocol requires property 'bEx' with type 'String.CompatibleType' (aka 'AExample<String>'); do you want to add a stub? var bEx: CompatibleType { get } ^ 20170520-221301.xcplaygroundpage:36:9: note: candidate has non-matching type 'BExample<Self>' [with CompatibleType = String.CompatibleType] var bEx: BExample<Self> { ^class Digest { let str: String init() { str = makeString() } private func makeString() -> String { return "hoge" } }
error: use of 'self' in method call 'makeString' before all stored properties are initializedprivate とはいえ、そこから public 呼べますからね。!! 演算子で明示的にクラッシュケースを書いた場...
class Digest() { val str: String init { str = makeString() } private fun makeString(): String { return "hoge" } }let と var でゼロコストでミュータビリティを変えられる Swift 最強let a = [1,2] // a = [1,2] var b = a; // b = [1,2] b[1] = 3; // a = [1,3] b = [1,3] b.append(5); // a = [1,3] b = [1,3,5] b[1] = 4; // a = [1,3] b = [1,4,5]class Digest { let str: String init(item: Digest) { str = item.makeString() } func makeString() -> String { return "test" } convenience init() { self.init(item: self) } } print(Digest().str) 完全にだめなやつw convenience init() { self.init(item: self) }self.initにselfが渡せるのがもうおかしい気がclass Digest { let str: String let hello: String init(item: Digest) { str = item.makeString() hello = "test" } func makeString() -> String { return hello } convenience init() { self.init(item: self) } } print(Digest().str) // "" <- ?Foo() 1(75709,0x70000aac2000) malloc: *** error for object 0x6180000129ec: Invalid pointer dequeued from free list *** set a breakpoint in malloc_error_break to debugモダンなSwiftのExtension ですけど、 UIColor.hoge は view.backgroundColor = .hoge できるけど UIColor.ex.hoge は view.backgroundColor = .ex.hoge できないってどうなんですかね?func mockFetch() -> Observable<Bool> { return Observable.create { observer in observer.on(.next(true)) observer.on(.completed) return Disposables.create() } }mockFetch().subscribe(onNext: { _ in print("done") }).disposed(by: disposeBag)Emits zero elements Emits a completion event, or an error Doesn't share side effects.subscribe(onComplete: {}) で拾うという手があるかも?ignoreElementsが使えそう<Void> にして気にしないとかcreate で書かずに .just でもかけますよ。下請けに本当の非同期処理がある場合は create ですねdiff "${PODS_PODFILE_DIR_PATH}/Podfile.lock" "${PODS_ROOT}/Manifest.lock" > /dev/null if [ $? != 0 ] ; then # print error to STDERR echo "error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation." >&2 exit 1 fiplatform :ios, '9.0' target 'XXXXX' do pod 'CorePlot', '2.2' pod 'Google-Mobile-Ads-SDK', '7.20.0' pod 'FBSDKCoreKit', '4.23.0' pod 'FBSDKLoginKit', '4.23.0' pod 'FBSDKShareKit', '4.23.0' pod 'Flurry-iOS-SDK/FlurrySDK', '8.1.0' pod 'Fabric', '1.6.11' pod 'TwitterKit', '2.8.1' pod 'TwitterCore', '2.8.0' pod 'Crashlytics', '3.8.4' pod 'SVProgressHUD', '2.1.2' pod 'Realm', '2.7.0' pod 'Repro', '2.4.0' pod 'SwiftLint', '0.19.0' end post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['CLANG_WARN_DOCUMENTATION_COMMENTS'] = 'NO' end end endPODS: - Bolts (1.8.4): - Bolts/AppLinks (= 1.8.4) - Bolts/Tasks (= 1.8.4) - Bolts/AppLinks (1.8.4): - Bolts/Tasks - Bolts/Tasks (1.8.4) - CorePlot (2.2) - Crashlytics (3.8.4): - Fabric (~> 1.6.3) - Fabric (1.6.11) - FBSDKCoreKit (4.23.0): - Bolts (~> 1.7) - FBSDKLoginKit (4.23.0): - FBSDKCoreKit - FBSDKShareKit (4.23.0): - FBSDKCoreKit - Flurry-iOS-SDK/FlurrySDK (8.1.0) - Google-Mobile-Ads-SDK (7.20.0) - Realm (2.7.0): - Realm/Headers (= 2.7.0) - Realm/Headers (2.7.0) - Repro (2.4.0) - SVProgressHUD (2.1.2) - SwiftLint (0.19.0) - TwitterCore (2.8.0): - Fabric - TwitterKit (2.8.1): - TwitterCore (>= 2.8) DEPENDENCIES: - CorePlot (= 2.2) - Crashlytics (= 3.8.4) - Fabric (= 1.6.11) - FBSDKCoreKit (= 4.23.0) - FBSDKLoginKit (= 4.23.0) - FBSDKShareKit (= 4.23.0) - Flurry-iOS-SDK/FlurrySDK (= 8.1.0) - Google-Mobile-Ads-SDK (= 7.20.0) - Realm (= 2.7.0) - Repro (= 2.4.0) - SVProgressHUD (= 2.1.2) - SwiftLint (= 0.19.0) - TwitterCore (= 2.8.0) - TwitterKit (= 2.8.1)SPEC CHECKSUMS: Bolts: 8a7995239dbe724f9cba2248b766d48b7ebdd322 CorePlot: cbe0c6e14220705e32e06a89c7d6a5a908b7d7d8 Crashlytics: 79e236942ca1e7fc641df1feb9a275360a78ab6a Fabric: 5911403591946b8228ab1c51d98f1d7137e863c6 FBSDKCoreKit: d7829f5a69901cd7296105328e4c3c3fc7da6518 FBSDKLoginKit: 6b92bd65f319d669d2b0730e49db495ef1660566 FBSDKShareKit: 65751c3b9937ab9d594e6c37611ecc115ceeb448 Flurry-iOS-SDK: 51065be4436f3e21fc8d3d7bb0b7d6869df6333d Google-Mobile-Ads-SDK: 168312cdc167fa22746d4ea8e839fc4825dd2966 Realm: 07e5d3a9e0e41d65fd5f9f05150ed6295d0f093b Repro: e69274efa79b1e8f25ec14177356968d9a8e8224 SVProgressHUD: c404a55d78acbeb7ebb78b76d3faf986475a6994 SwiftLint: 3537a05b34060e78e7510f04fb7537d738247803 TwitterCore: e959987be23c767004d29eb7fab8d85d8585a2e5 TwitterKit: 3b6044a7320340326a4098f3e37aa1dbaeb94942 PODFILE CHECKSUM: 47c07dc78a6793f29d1576ffa394f4c5e5b5076e COCOAPODS: 1.2.1diff "${PODS_PODFILE_DIR_PATH}/Podfile.lock" "${PODS_ROOT}/Manifest.lock" > /dev/null if [ $? != 0 ] ; then # print error to STDERR echo "error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation." >&2 exit 1 fiecho "error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation. このメッセージは出て無くて、差分はないと思います。URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil) で解決しました(この方法だとアプリ全体でキャッシュが効かなくなりますが、画像周りは独自のキャッシュを実装していたので今回は影響がなかったNSAttributedStringで、リンクをタップした際の挙動をSafariを開く挙動から自分で定義した挙動に変更するにはどうしたらいいのでしょうClean Clean build folder Quit Xcode Open Finder then go to ~/Library/Developer/Xcode/DerivedData then deleated everything inside Open Xcode Build# frozen_string_literal: true # A sample Gemfile source "https://rubygems.org" gem "fastlane" gem 'cocoapods' gem 'cocoapods-keys'source "https://rubygems.org" gem "cocoapods", '1.2.1' gem "fastlane", '2.11.0'let storyboard = UIStoryboard(name: "ViewController", bundle: Bundle.main) let vc = storyboard.instantiateInitialViewController() as! ViewController vc.inject([1, 2, 3])
import Instantiate import InstantiateStandard extension ViewController: StoryboardInstantiatable {} let vc = ViewController(with: [1, 2, 3])
ViewController.init(with:) を 呼ぶことを忘れなければうまくいくけどinject みたいに「あるメソッドを通過」が外から呼び出すことを意図しているなら、 assert を書く人と呼び出す人が別な気がするので「assertはいらない」ことにならない気がするんですが・・・。
(edited)@available(*, unavailable, scope: public) 的な (edited)Username for 'https://github.com': omochi Password for 'https://omochi@github.com': remote: Invalid username or password.Via HTTPS For those checking out sources as read-only, HTTPS works best: git clone https://github.com/apple/swift.git ./swift/utils/update-checkout --clone Via SSH For those who plan on regularly making direct commits, cloning over SSH may provide a better experience (which requires uploading SSH keys to GitHub): git clone git@github.com:apple/swift.git ./swift/utils/update-checkout --clone-with-sshimport UIKit let numbers = (1...5).map(String.init) print(numbers) // ["1", "2", "3", "4", "5"] var a: [String] = [] // No Error a = Array(numbers.prefix(3)).map { $0 } print(a) // ["1", "2", "3"] // No Error let arraySlice: ArraySlice<String> = numbers.prefix(3) a = arraySlice.map { $0 } print(a) // ["1", "2", "3"] // Error(error: ambiguous use of 'prefix')→ なぜエラーに? // a = numbers.prefix(3).map { $0 }numbers.prefix(3).map { $0 } が ambiguous use of 'prefix' になる理由がわからず。NSImage を生成して tiffRepresentation を呼ぶと nil が返ってきてしまうんですが、解決策をご存知の方はいませんか? 今やっているのは↓で、 let image = NSImage(cgImage: cgImage, size: NSSize.zero) image.tiffRepresentation // nil この cgImage は↓のように生成されています。 extension Image where Pixel == RGBA { public var cgImage: CGImage { let length = count * 4 // count == width * height var data = Data(capacity: length) data.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) -> Void in // 略 } let provider: CGDataProvider = CGDataProvider(data: data as CFData)! return CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: width * 4, space: Image.colorSpace, bitmapInfo: Image.bitmapInfo, provider: provider, decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent)! } fileprivate static var colorSpace: CGColorSpace { return CGColorSpaceCreateDeviceRGB() } fileprivate static var bitmapInfo: CGBitmapInfo { return CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue) } }
tiffRepresentation のドキュメントには↓のようにありますが、特に PDF や EPS のようなベクター形式の representation にはなっていないと思います。 "if the TIFF data cannot be created" の原因がわからず・・・。 If one of the receiver's image representations does not support the creation of TIFF data natively (PDF and EPS images, for example), this property creates the TIFF data from that representation's cached content. This property contains nil if the TIFF data cannot be created.
https://developer.apple.com/documentation/appkit/nsimage/1519841-tiffrepresentation なお、同様の手法↓で生成されたグレースケールの CGImage から作られた NSImage や、 PNG ファイルを読み込んで生成された NSImage の tiffRepresentation は正しく動作します。 https://github.com/koher/EasyImagy/blob/05db8c28d034a36cef3a8271d2daa214c8ad5ee4/EasyImagy/ImageCoreGraphics.swift#L133-L147 var data = Data(length: length) で初期化しないと、capacity あっても直接イニシャライズすると length は 0 のままなので。 (edited)init(capacity:) で生成して、 data.length = length で拡張するか。← データ突っ込んだ後にやると 0 で初期化されるので注意 (edited)Data.init(count:) だ。 issue からリンクされているプロジェクトでは NSMutableData だった。NSMutableData(length:)で正常に取得できることを確認しました。capacityとlength/countは盲点でした…… ポインタと合わせる場合は特に要注意ですねData の使い方を間違えている可能性をまったく考えませんでした・・・。UIView.animate(withDuration: 1.0, animations: { someView.alpha = 0.0 }) { _ in someView.removeFromSuperview() }UIView.animate( withDuration: 1.0, animations: { someView.alpha = 0.0 }, completion: { _ in someView.removeFromSuperview() } )var dict1 = ["May 21": [1,2], "May 22": [3,4]] var dict2 = ["May 22": [5,6], "May 23": [7,8]] dict1.merge(dict2, uniquingKeysWith: +) print(dict1) // ["May 22": [3, 4, 5, 6], "May 23": [7, 8], "May 21": [1, 2]]Swift Standard Library.playground とか https://developer.apple.com/documentation/swift ここのドキュメントとか https://developer.apple.com/jp/documentation/a とか b とか付けてたので。概念自体は理解できると思いますが、(自然)言語の壁がありそうです・・・。int が Integer に[NSValue valueWithInt: 3] とかやってたけど、 iOS5ぐらいから @(5) って、アット丸括弧構文でboxingできるようになったとか、昔はあった// Java List<Integer> list = new ArrayList<Integer>(); list.add(new Integer(42)); // boxing// Java List<Integer> list = new ArrayList<Integer>(); list.add(42); // auto boxingint に対して Integer をラッパークラスと言います。C.self is AnyObject 問題があったから、 Swift の型システムとして表現したい世界と、 ObjC 互換のために現実的にどうなってるかとを区別して考えないといけないかも?struct S{} var s = S() print(type(of: s as AnyObject)) // _SwiftValue var f: () -> Int = { 33 } print(type(of: f as AnyObject)) // _SwiftValue ↑SwiftValueの例#if __OBJC__ @class _SwiftValue; #else typedef struct _SwiftValue _SwiftValue; #endif#if SWIFT_OBJC_INTEROP で @interface _SwiftValue : NSObject <NSCopying> だから is AnyObject が Mac だと true になるんだ。 (edited)S.self でもそのはず?class List<T: AnyObject> {} これがJavaのListの定義class ArrayList { private Object[] elements; } こんな風になってたのをclass ArrayList<E> { private E[] elements; } と書いているだけなので、int[] とか float[] みたいな、値型の配列も作れるんだけど、 List<int> のときにそれを内部で使わせる記法も仕組みもない あと、これは鶏卵だけど、ジェネリクスの型パラメータのところに値型が書けない・・・Object で持ってるのと同じです。コンパイル時に型チェックできるようになっただけで、実行時の挙動はジェネリクス以前と同じで Object です。List<int> を導入する話が出てたような?List は List<Object> と書いてるのと同じですね。List<Integer> a = new ArrayList<Integer>(); List<String> s = (List<String>)(List<Object>)a;
[Int]() as? [String] これを嫌がる理由がint foo(List<Integer> list) { ... } int foo(List<Double> list) { ... }associatedType ?let cell = tableView.dequeueReusableCell(withIdentifier: "hogeCell", for: indexPath) ?? UITableViewCell(style: .subtitle, reuseIdentifier: "hogeCell") ③ その他 ただstyleが違うだけなのにイニシャライザだけ定義した、UITableViewCellのサブクラスつくるのが面倒なので良い方法があったりしないかな…と思った感じです。 (edited)indexPath を2番目の引数に取る dequeueReusableCell() はnilを返さないので、2の書き方はindexPath を取らない方のメソッドを呼ぶ必要があります。細かいツッコミですけど。Frameworksの下に参照が作られるから、それをEmbedded~へ入れる様にしてる。[Int: Any] を [String: Any] にしたいみたいなときってどう書くのが一番綺麗なんでしょうlet source = [1: "1", 2: "2", 3: "3"] Dictionary(uniqueKeysWithValues: source.map { ("\($0.key)", $0.value) }) こう?addSubview(_:)とremoveFromSuperView()をしてしまうと、結果としてビューが外された最後の状態でUIが更新されます。download()は同期的な処理ですか?最も望ましいと思われる解決はdownload()メソッドを非同期処理にして完了時点でコールバックを受け取れるようにして、addSubview(_:) を呼んでからdownload()、ここまでは同じで、removeFromSuperView()をダウンロード完了のコールバック内で呼ぶことです。addSubview(_:)の直後に一度イベントループに処理を戻してUIを更新して、download()、removeFromSuperView()を呼びます。view.addSubview(uiView) DispatchQueue.main.async { self.download() uiView.removeFromSuperView() }protocol A { associatedtype TypeA } protocol B { associatedtype TypeB } protocol C: A, B where TypeA == TypeB { } struct Hoge: C { // error: type 'Hoge' does not conform to protocol 'A' typealias Type2 = String }struct Hoge: C { typealias TypeA = String typealias TypeB = String } これなら通りますね。TypeAとTypeBが別々だと通らなくなるので同一チェックもされてるようです。typealias を書かなくてもコンパイルは通ります。 protocol A { associatedtype TypeA func foo() -> TypeA } protocol B { associatedtype TypeB func bar() -> TypeB } protocol C: A, B where TypeA == TypeB { } struct Hoge: C { func foo() -> Int { return 2 } func bar() -> Int { return 3 } }protocol A { associatedtype TypeA } protocol B { associatedtype TypeB } protocol C: A, B { associatedtype TypeB = TypeA } struct Hoge: C { typealias TypeA = String }C のそこ associatedtype になるんですね。 typealias で書いてエラーになってあきらめてました。protocol A { associatedtype E } protocol B { associatedtype E } struct C: A, B { typealias A.E = Int typealias B.E = String } これはどうあがいてもムリです。 (edited)CFNetwork URLResponse::guessMIMETypeCurrent(__CFData const*) のバグに苦しめられてまして、どなたか同じような現象に出くわした方いらっしゃいませんか。 MIMETypeについてなので、URLResponseをNSHTTPURLResponseにキャストしてMIMETypeの中身を見てるんですが、たまにnullが返ってくるのが問題かなと思ったのですが、iOS10や9でもnullで返ってくるので、特にこれが原因でクラッシュするわけではないような…。 ググっても同じような問題に悩んでいる人が見つからなかった…。 Crashed: com.apple.NSURLSession-work SIGABRT ABORT 0x0000000180ded348 -------------------------------------------- libsystem_kernel.dylib __pthread_kill + 8 CoreFoundation _CFRelease + 1252 CFNetwork URLResponse::guessMIMETypeCurrent(__CFData const*) + 1548 CFNetwork -[__NSCFURLLocalSessionConnection _tick_sniffNow] + 348 libsystem_pthread.dylib start_wqthread + 4guessMIMETypeCurrent だからContent-Typeを付け足してみて試すとか。POST / HTTP/1.1 [[ Less interesting headers ... ]] Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150 Content-Length: 834 -----------------------------735323031399963166993862150 Content-Disposition: form-data; name="text1" text default -----------------------------735323031399963166993862150 Content-Disposition: form-data; name="text2" aωb -----------------------------735323031399963166993862150 Content-Disposition: form-data; name="file1"; filename="a.txt" Content-Type: text/plain Content of a.txt. -----------------------------735323031399963166993862150 Content-Disposition: form-data; name="file2"; filename="a.html" Content-Type: text/html <!DOCTYPE html><title>Content of a.html.</title> -----------------------------735323031399963166993862150 Content-Disposition: form-data; name="file3"; filename="binary" Content-Type: application/octet-stream aωb -----------------------------735323031399963166993862150--if (image){ のとこではContent-Type指定してあるんですね。CFNetwork -[__NSCFURLLocalSessionConnection _tick_sniffNow] + 348 でNSのプレフィックスが付いているのでobjcから呼ばれた通信でバグっているのはあっているのでしょうか?CoreFoundation _CFRelease + 1252 こっちじゃないかなあAfter you create a task, you start it by calling its resume method. The session then maintains a strong reference to the task until the request finishes or fails; you do not need to maintain a reference to the task unless it is useful to do so for your app’s internal bookkeeping purposes. 大丈夫そうだなあ・・・UIImage の↓の二つのプロパティは、ドキュメントを素直に読めば片方が nil のときはもう片方は nil でないということになると思うんですが、それは保証されているんでしょうか? var cgImage: CGImage? { get }
var ciImage: CIImage? { get }
! に失敗しない)んでしょうか?より良い書き方がありますか? if let cgImage = uiImage.cgImage { // `cgImage` を使う処理 } else { let ciImage = uiImage.ciImage! // forced unwrap // `ciImage` を使う処理 } (edited)Playground で再現するのですが、 UIImage のインスタンスがあっても どっちも nil の場合はあるみたいですね let image = UIImage() switch (image.ciImage, image.cgImage) { case (let ciImage?, nil): print("ciImage: \(ciImage)") case (nil, let cgImage?): print("cgImage: \(cgImage)") default: print("none") } // none
CIImage, CGImage において、 どちらかで init すると片方が nil になるとは書いてありますが、 それ以外の関係性が書いてないので switch で書いたのは単純に好みなのですが、 こっちの方が網羅性があるので CIImage, CGImage どっちかがあれば とう意味では読みやすいかなと思いますnil になりますね・・・。NULL / nil にならないとは言ってないってことですね。
UIImage の init() ってObj-C の NSObject 由来で、 public に使われることは想定されてないんでしょうか?ドキュメントにも記載されてない・・・。 https://developer.apple.com/documentation/uikit/uiimageUIImage() 作って渡してた記憶が…nil 渡すとデフォルト画像が使われちゃうので UIImage() で渡してたはず、なんのコンポネントかは忘れたけどextension Image where Pixel == RGBA<UInt8> { public init(uiImage: UIImage) { #if os(iOS) || os(tvOS) if let cgImage = uiImage.cgImage { self.init(cgImage: cgImage) } else if let ciImage = uiImage.ciImage { let context = CIContext() // This `guard` can be replaced with `!` if you are sure that the `createCGImage` below never fails. guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { fatalError("Failed to create a `CGImage` from an internal `CIImage` object from this `UIImage` instance: \(uiImage)") } self.init(cgImage: cgImage) } else { // This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`. guard uiImage.size == .zero else { fatalError("The `size` of this `UIImage` instance (\(uiImage)) is not equal to `.zero` though both `cgImage` and `ciImage` of the instance are `nil`.") } self.init(width: 0, height: 0, pixels: []) } #else if let cgImage = uiImage.cgImage { self.init(cgImage: cgImage) } else { // This `gurad` can be replaced with `assert` if you are sure that the `size` is always equal to `.zero`. guard uiImage.size == .zero else { fatalError("The `size` of this `UIImage` instance (\(uiImage)) is not equal to `.zero` though `cgImage` of the instance is `nil`.") } self.init(width: 0, height: 0, pixels: []) } #endif }
#if で } else if ... { を落とすことができなくて悲しい・・・。CoreImage 使ったことなかったんですが、↓を発見して頭を抱えてますpublic init(uiImage: UIImage, clampedTo rect: CGRect? = nil) とかはありかもしれないけど、 fatalError が順当そう。CFNetwork URLResponse::guessMIMETypeCurrent(__CFData const*) のバグを質問したものですが、バグの解消できました!いろいろアドバイスくれた方々、本当にありがとうございました!いろいろ対策をしたのでどれが解決になったのかわかりませんが、おそらく URLSessionをsharedで呼べばいいのに不要にConfigurationを組み立てたり、奇妙なタイミングで finishTasksAndInvalidate している通信クラスがあり、そいつが原因だったように思います。お騒がせしました! (edited)[omochi@omochi-iMac tt]$ cat a.swift let a: Int = 3 let b: Int = 2 print(b) [omochi@omochi-iMac tt]$ swiftlint Linting Swift files in current working directory Linting 'a.swift' (1/1) /Users/omochi/temp/tt/a.swift:1:1: error: Identifier Name Violation: Variable name should be between 3 and 40 characters long: 'a' (identifier_name) /Users/omochi/temp/tt/a.swift:3:1: error: Identifier Name Violation: Variable name should be between 3 and 40 characters long: 'b' (identifier_name) Done linting! Found 2 violations, 2 serious in 1 file.[C] のチェックを外すと@available(*, unavailable) をつけてみると@available(*, unavailable)つけて、エラーが出たものを@available(*, deprecated)に置き換えて残ったものを消す、deprecatedは使ってる箇所でwarningになるのでグローバル変数を減らしていく、とするとうまくいくのかなGCC_WARN_UNUSED_FUNCTION GCC_WARN_UNUSED_LABEL GCC_WARN_UNUSED_PARAMETER GCC_WARN_UNUSED_VALUE GCC_WARN_UNUSED_VARIABLE っていう設定がありますが、 Swift には効かないです。aと 全角 aみたいな。 (edited)let stringA = "123@456" let stringB = "123@456" let rangeA = stringA.range(of: "@[0-9]", options: .regularExpression, range: nil, locale: nil) //.some let rangeB = stringB.range(of: "@[0-9]", options: .regularExpression, range: nil, locale: nil) //nil
.range ならNS通らなくて済むからできそうですね internal func _rangeOfRegularExpressionPattern(regex pattern: String, options mask: CompareOptions, range searchRange: NSRange, locale: Locale?) -> NSRange { var matchedRange = NSRange(location: NSNotFound, length: 0) let regexOptions: NSRegularExpression.Options = mask.contains(.caseInsensitive) ? .caseInsensitive : [] let matchingOptions: NSRegularExpression.MatchingOptions = mask.contains(.anchored) ? .anchored : [] if let regex = _createRegexForPattern(pattern, regexOptions) { matchedRange = regex.rangeOfFirstMatch(in: _swiftObject, options: matchingOptions, range: searchRange) } return matchedRange }func isValidEmailFormat(string: String) -> Bool { let emailReg = "[a-zA-Z0-9._%+-/:~$^()&!']+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" let predicateMail = NSPredicate(format:"SELF MATCHES %@", emailReg) return predicateMail.evaluate(with: string) } (edited)NSPredicate(format:"SELF MATCHES %@", emailReg) おおNSPredicate式だfunc isValidEmailFormat2(string: String) -> Bool { let emailReg = "[a-zA-Z0-9._%+-/:~$^()&!']+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" // valid regexp let regexp = try! NSRegularExpression.init(pattern: emailReg, options: []) let nsString = string as NSString let matchRet = regexp.firstMatch(in: string, options: [], range: NSRange.init(location: 0, length: nsString.length)) return matchRet != nil } isValidEmailFormat2(string: "aaa@aaa.com") isValidEmailFormat2(string: "aaa@aaa.com").init は消せそう。[a-zA-Z0-9._%+-/:~$^()&!'] は . と (最後の)- をエスケープしないと意図とちがうことになりそう (edited)// valid regexp は try! にかかってるのかJSONSerialization で一度 Data 化してから JSONDecoder を利用する方法かなと思いますが JSONDecoder 内で JSONSerialization で Dictionary というか Any 化されてるんですね。 https://github.com/apple/swift/blob/5836ea4d11ff5eb66f9c74ec1c0202585a467a9d/stdlib/public/SDK/Foundation/JSONEncoder.swift#L1060 なんか非効率だなーと感じるので、他に良い方法があれば知りたいです。最悪 JSONDecoder を丸々コピって DictionaryDecoder みたいなものを作る事になるのかなと思うんですが JSONDecoder が同じ処理をしてると思うとそれも非効率に感じます。。[String: T] where T : Codable に encode/decode するライブラリとかあるといいのになと思います。Codable にあまり詳しくないので、何かいい方法があるかもしれません。 @norio_nomura さんとか詳しそうです。[String: Any], [Any] or Any as payload.ObjectEncoder 使わせてもらおうとしたんですが、JSONEncoder.DateEncodingStrategyに相当するものが無いのに気づきDateEncodingStrategyとか要りますかね。僕の用途では不要だったので付けなかったのですが…DateDecodingStrategyの方か。DateDecodingStrategy .deferredToDate 扱いか (edited)StringからDate?を作るメソッドを指定できる仕組みがあれば良いのかな?Anyからか。keyEncodingStrategyとかも既にSwift 4.1に入ってるのか。 import Foundation let encoder = JSONEncoder() encoder.keyEncodingStrategy = .convertToSnakeCaseObjectEncoderで任意の型にCoding Strategyを設定できる様になりそう。 https://github.com/norio-nomura/ObjectEncoder/pull/11
var encoder = ObjectEncoder() encoder.encodingStrategies[Date.self] = .iso8601 let encoded = try encoder.encode(Date(timeIntervalSince1970: 1000)) encoded == "1970-01-01T00:16:40Z" // true var decoder = ObjectDecoder() decoder.decodingStrategies[Date.self] = .iso8601 let decoded = try decoder.decode(Date.self, from: encoded) decoded == Date(timeIntervalSince1970: 1000) // true (edited)Foo を作って 2. module.modulemap ファイルを作って、アンブレラヘッダー( <Foundation/Foundation.h>のようなもの)を作って参照できるように書く module Foo { header "../Foo.h" export * } 3. SWIFT_INCLUDE_PATHSにモジュールまでのパス(Fooディレクトリまでのパス)を設定する 4. import FooできればOKmodule.modulemapは極力使わない様にしてる。SwiftPM対応しないなら関係ないけど。#include の世界が シンボルベースの世界に変換されてmodule.modulemapを自動生成します。 (edited)@_exported import の裏技とかは、stringByDefaultEncodingWithData の呼び出しまでしか確認してないけど、modulemapファイルにlibiconvをリンクする設定がいるかも。NSString+iconv.h/mを1つのモジュール(IconvWrapper.framework)にしてそれをsample.framework(およびTestApp)からインポート。import IconvSwift class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let data = Data.init(bytes: [0x82, 0xA0]) // SJIS で "あ" let str = NSString.init(byDefaultEncodingWith: data) print(str) } ↑で、これが動いたので、OKswift package generate-xcodeprojで再生成したものを修正してる。let intIconv: iconv_t = iconv_open("UTF-16//IGNORE", "SHIFT-JIS") guard Int(bitPattern: intIconv) != -1 else { throw EncodingErrors.invalidEncodingName }struct Card { enum Suit { case spade case heart case club case diamond } enum Rank { case one case two case three case four case five case six case seven case eight case nine case ten case j case q case k } let suit: Suit = .heart let rank: Rank = .j init(suit: Suit, rank: Rank) {} } let card = Card(suit: .spade, rank: .k)let suit: Suit = .heart と書くと init では代入できない。init(suit: Suit, rank: Rank) {} で渡しているパラメータは self.suit には代入できないので別のことに使うしかない。import UIKit class ViewController: UIViewController { var provider = Provider() override func viewDidLoad() { super.viewDidLoad() provider.reload { self.provider.layout() // 1) Simultaneous access ! } } struct Provider { let cache = Cache() // 2) comment out mutating func reload(completion: @escaping ()->Void) { // 3) remove mutating keyword completion() } func layout() { } } class Cache { } } (edited) override func viewDidLoad() { super.viewDidLoad() provider.reload() } struct Provider { var cache = Cache() mutating func reload() { layout() } func layout() { } }No enforcement is required for immutable memory, like a let binding or property, because all accesses must be reads.Int.advanced(by:) や Array.filter(_:) のような、同じ型の新しいインスタンスを返すメソッドのことを、ゲッター、セッター、ミューテーターみたいな分類でいうと何と言いますか? (edited)extension UIView { var ii : Int { get { return 0 } set(i) { // hogehoge } } } let v = UIView() v.ii = 3 propertyがこんがらがってしまって、Stored property と Computed propertyがあって上のコード内のiiはComputed propertyであってますでしょうかii は Computed Property です。// Stored property. var p1: Int = 1 // Computed property. var p2 Int { get { return 1 } set { /* ... */ } } // Computed property ( 暗黙のgetのみ ) var p3: Int { return 1 } (edited)extension UIView { var a = 0 }extension UIView { var ii : Int { get { return 0 } set(i) { backgroundColor = UIColor.red } } } と、classにある値を変更するのは、よくないでしょうか?set した値を get で取り出せるようにすべきだと思いますよ。func example<T>(_ type: T.Type) { }
example(Int.self)example<Int>() こう呼べても良いような気もちょっとだけする(T.Type) -> (T) -> ()でexample(Int)とかできませんかねfunc example<T>(type: T.Type=T.self) -> (T) -> () { return { arg in print(arg) } } let intE = example(type: Int.self)intE は Int が引数の関数でしょexample<Int>() で呼べるのかfunc example<T>(arg: T) -> () { print(arg) } func example<T>(type: T.Type=T.self) -> (T) -> () { return example } let intE = example(type: Int.self)honest がすでにBool型なところにnilを入れたいということなら Bool? なり適切なモデル型なりに型を変える必要ありそうJSONSerialization とかでgrepしてみたらどうでしょうか<=だったかな、うろ覚えですが public subscript(key: String, nested nested: Bool, ignoreNil ignoreNil: Bool) -> Map { // save key and value associated to it currentKey = key keyIsNested = nested // check if a value exists for the current key // do this pre-check for performance reasons if nested == false { let object = JSONDictionary[key] let isNSNull = object is NSNull print(object) isKeyPresent = isNSNull ? true : object != nil currentValue = isNSNull ? nil : object } else { // break down the components of the key that are separated by . (isKeyPresent, currentValue) = valueFor(ArraySlice(key.componentsSeparatedByString(".")), dictionary: JSONDictionary) } // update isKeyPresent if ignoreNil is true if ignoreNil && currentValue == nil { isKeyPresent = false } return self }Non-public API usage で、bitcode有効にすると通りましたextension<T: SomeProtocol> Array where Element == T みたいなジェネリックなエクステンションってまだできないですよね?- Added the Conditionally Conforming to a Protocol section with information about conditional protocol conformance. - Added information about recursive protocol constraints to the Using a Protocol in Its Associated Type’s Constraints section. - Added information about the canImport() and targetEnvironment() platform conditions to Conditional Compilation Block.LIO...
|[ A ] [ B ]| | [ C ]| こういう感じで、BとCがそれぞれ可変サイズだとして、Aの右端が、BかCのどちらかより長い方の左端にくっつく ってAutoLayoutでどうやって書くんです? (edited)|[A]-(>=10@1000)-[B]| |[A]-(>=10@1000)-[C]| |[A]-(0@750)-| ではだめですかね? (edited)Even if an optional constraint cannot be satisfied, it can still influence the layout. If there is any ambiguity in the layout after skipping the constraint, the system selects the solution that comes closest to the constraint. In this way, unsatisfied optional constraints act as a force pulling views towards them.blue.right = max(stack.left, long.left, greater.left, long2.left) 的な感じで書けそう (edited)blue.right = max(stack.left, long.left, greater.left, long2.left) ってかいたらそのまんまやりたいことがAutoLayout制約としてセットアップされたら一番嬉しいlayoutSubviews() の中に書いとけばあとは自動でやってくれるじゃん?label.sizeThatFits(label.size) を取れば大きい方はわかる…はずletを使うとDecoder.decodeで読むデータにそのプロパティに対応するものがあってもなくても初期値が入る(例外も出ない)、という挙動を見つけたのですが、この挙動ってどうなんでしょうか import Foundation class Hoge: Codable { let fuga = "default value" private(set) var privateSet = "default value" } let decoder = JSONDecoder() decoder.decode(Hoge.self, from: "{\"fuga\": \"json value\", \"privateSet\": \"json value\"}".data(using: .utf8)!)private(set) varを使えば想定する動きをすることがわかったのでそちらを使っているのですが、初期値なしletは正常に動作するので、そちらと混ぜるとコードがデコボコして見栄えがよろしくないことになってしまってさらに letのためですね。letでデフォルト値が与えられているのでそれ以上代入することができないのです。varにして試してみてください。たぶん、init(decoder:)を書いてみるとわかると思います。-Wl,-stack_size,1000000 to the Other Linker Flags field of the Build Styles pane. ld: -stack_size option can only be used when linking a main executable clang: error: linker command failed with exit code 1 (use -v to see invocation) なるものが出てしまった…AppDelegateだけの空っぽのアプリで構わないです。テスト中に生きてさえいればいい。swift testの場合はわからないです。HOST_APPっていう環境変数がきくかもしれない(たぶんきかない)。 (edited)objc[29923]: Class _TtC8Dispatch16DispatchWorkItem is implemented in both /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/libswiftDispatch.dylib (0x1018f7530) and /Users/taketo1024/Library/Developer/Xcode/DerivedData/TimeProfile-aryrvmqtlkmqgqbautymimyzbktk/Build/Products/Debug/TimeProfile (0x1005c6820). One of the two will be used. Which one is undefined. こういうのがたくさん出て実行はできるのですが print の結果がおかしくなりますSWIFT_FORCE_DYNAMIC_LINK_STDLIB=YESとSWIFT_FORCE_STATIC_LINK_STDLIB=NOをUser Defined Settingで追加してみてください。func updateTableView() { ... tableView.reloadData() // 1 ... tableView.scrollTo(latestIndexPath) // 2 ... } のようになっています。protocol A where Y.X == Self, Y.Z == Z { // Redundant same-type constraint 'Self.Z' == 'Self.Y.Z' associatedtype Y: B associatedtype Z } protocol B where X.Y == Self, X.Z == Z { // Redundant same-type constraint 'Self.Z' == 'Self.X.Z' associatedtype X: A associatedtype Z } struct AA: A { typealias Y = BB typealias Z = Int } struct BB: B { typealias X = AA typealias Z = String // !? } このコードが通ってしまうんですが(本来は BB.Z == AA.Z == Int でなければいけない)protocol の associatedType の相互参照周りはまだバグバグしいんでしょうか? (edited)AとB、 AA と BB で循環しているので、循環まわりはまだまだ安定していない印象test.swift:11:8: error: 'A' requires the types 'AA.Z' (aka 'Int') and 'BB.Z' (aka 'String') be equivalent struct AA: A { ^ test.swift:11:8: note: requirement specified as 'Self.Z' == 'Self.Y.Z' [with Self = AA] struct AA: A { ^
1protocol ViewProtocol: class { associatedtype Presenter: PresenterProtocol var presenter: Presenter! { get } } protocol PresenterProtocol: class { associatedtype View: ViewProtocol weak var view: View? { get } } 1ファイルにこれ書くとbuild通るんですけど、それぞれのprotocolを別のファイルに書くとbuild通らないのがよくわからないんですがどうしてなんでしょう? (edited)protocol A where Y.X == Self, Y.Z == Z { // Redundant same-type constraint 'Self.Z' == 'Self.Y.Z' associatedtype Y: B associatedtype Z } protocol B where X.Y == Self, X.Z == Z { // Redundant same-type constraint 'Self.Z' == 'Self.X.Z' associatedtype X: A associatedtype Z } struct AA: A { typealias Y = BB typealias Z = Int } struct BB: B { typealias X = AA typealias Z = String // !? } ↑こちら Xcode 9.3 beta で試してみたらちゃんとエラーになりましたstruct A { var name = "a" } print(A()) struct B { var name = "b" } extension B: CustomDebugStringConvertible { var debugDescription: String { return "{\(name)}" } } print(B())Swift version 4.1 (swift-4.1-RELEASE)A(name: "a") {b} 4> let a = A() a: A = { name = "a" } 5> struct B { 6. var name = "b" 7. } 8. 9. extension B: CustomDebugStringConvertible { 10. 11. var debugDescription: String { 12. return "{\(name)}" 13. } 14. } 15> let b = B() b: B = { name = "b" } あれ、REPL だとやっぱり変わらないようなHello Worldまでの手順は記事がいっぱいあるので割愛します。 サーバーサイドに慣れてないSwift開発者だけど、自分でもできそう! ...while true { } って書いてみましたがずっと動いてるようですwSwift Tweets 2018 Spring 2018-04-14 21:00 JST "Swift Playgroundを作ろう" @k_katsumienum UnrecoverableError: Error { case preconditionFailure } struct Hoge { func テストしたいメソッド() throws { if true { throw UnrecoverableError.preconditionFailure } } } let hoge = Hoge() do { try hoge.テストしたいメソッド() } catch let error as UnrecoverableError { switch error { case .preconditionFailure: preconditionFailure() } } うーんやりたくないfunc hoge() -> Never { return unsafeBitCast((), to: Never.self) } guard 1 == 0 else { hoge() } print("離脱してないよー")Playground execution failed: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation. * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) * frame #0: 0x00007fffaba7aa9b libobjc.A.dylib`_objc_msgSend_uncached + 11 frame #1: 0x00007fff9865d231 Foundation`-[NSPlaceholderString initWithString:] + 107 frame #2: 0x000000010c5d4007 PlaygroundLogger`function signature specialization <Arg[0] = Dead> of static PlaygroundLogger.LoggingPolicyStack.get() -> PlaygroundLogger.LoggingPolicyStack + 151 frame #3: 0x000000010c5cdf22 PlaygroundLogger`function signature specialization <Arg[1] = Owned To Guaranteed and Exploded> of playground_log_hidden + 98 frame #4: 0x000000010c5cd65b PlaygroundLogger`playground_log_hidden + 27 frame #5: 0x000000010c5fc094 $__lldb_expr82`hoge() at MyPlayground.playground:2unsafeBitCast((), to: Never.self) これでNever型のインスタンス作れるの @Yuta Saito が気づいたfunc coodinator() -> NSPersistentStoreCoordinator { do { return try makeCoodinator() } catch { //エラー処理 アラートを出してterminate } } という関数であった場合 func coodinator(_ f: () throws -> NSPersistentStoreCoordinator) -> NSPersistentStoreCoordinator { do { return try f() } catch { //エラー処理 アラートを出してterminate } } にしてテスト時にthrowする関数を与えるような感じ? だとおかしいですね... ちょっと考えてみます#if TEST func myPrecondition() -> Never { ... } #else func myPrecondition<T: FatalObjectCompatible>() -> T { return T.fatal() } #endif-Xfrontend -debug-time-function-bodies や -Xfrontend -debug-time-expression-type-checking が使えます。import Foundation public protocol MapType { associatedtype Domain associatedtype Codomain init(_ fnc: @escaping (Domain) -> Codomain) func applied(to x: Domain) -> Codomain } public struct Map<Domain, Codomain>: MapType { internal let fnc: (Domain) -> Codomain public init(_ fnc: @escaping (Domain) -> Codomain) { self.fnc = fnc } public func applied(to x: Domain) -> Codomain { return fnc(x) } } public protocol EndType: MapType where Domain == Codomain { static var identity: Self { get } func composed(with f: Self) -> Self } public extension EndType { public static var identity: Self { return Self{ x in x } } public func composed(with g: Self) -> Self { return Self { x in // ココで EXC_BAD_ACCESS self.applied(to: g.applied(to: x)) } } } extension Map: EndType where Domain == Codomain { } public extension Array where Element: EndType { func composed() -> Element { return self.reduce(Element.identity) { (res, f) in res.composed(with: f) } } } let f = Map { x in 2 * x } let g = [f, f].composed() print(g.applied(to: 0)) 関数(写像)を抽象化した MapType と、具体型の Map: MapType を作り、特に Domain(定義域) と Codomain(値域) が一致している場合に EndType: MapType という subprotocol を用意して、配列に入った関数をまとめて合成できるようにしたいと思って上のようなコードを書いたのですが、実行時に EXC_BAD_ACCESS が出てしまいます FooType より FooProtocol という命名の方が主流だと思います。標準ライブラリがそのように変わったので。↓など。 https://developer.apple.com/documentation/swift/iteratorprotocolimport Foundation public protocol MapType { associatedtype DomainType associatedtype CodomainType init(_ fnc: @escaping (DomainType) -> CodomainType) func applied(to x: DomainType) -> CodomainType } public struct Map<Domain, Codomain>: MapType { internal let fnc: (Domain) -> Codomain public init(_ fnc: @escaping (Domain) -> Codomain) { self.fnc = fnc } public func applied(to x: Domain) -> Codomain { return fnc(x) } } public protocol EndType: MapType where DomainType == CodomainType { static var identity: Self { get } func composed(with f: Self) -> Self } public extension EndType { public static var identity: Self { return Self { x in x } } public func composed(with g: Self) -> Self { return Self { x in // ココで EXC_BAD_ACCESS self.applied(to: g.applied(to: x)) } } } extension Map: EndType where Domain == Codomain { } public extension Array where Element == Map<Int, Int> { func composed() -> Element { return self.reduce(Element.identity) { (res, f) in res.composed(with: f) } } } let f: Map<Int, Int> = Map { x in 2 * x } let fs: Array<Map<Int, Int>> = [f, f, f] let g: Map<Int, Int> = fs.composed() let t: Int = g.applied(to: 1) print(t)Swift version 4.1 (swift-4.1-RELEASE)8import Foundation public protocol EndType where DomainType == CodomainType { associatedtype DomainType associatedtype CodomainType init(_ fnc: @escaping (DomainType) -> CodomainType) func applied(to x: DomainType) -> CodomainType static var identity: Self { get } func composed(with f: Self) -> Self } public extension EndType { public static var identity: Self { return Self { x in x } } public func composed(with g: Self) -> Self { let fnc: (DomainType) -> DomainType = { x1 in let x2: DomainType = g.applied(to: x1) let x3: DomainType = self.applied(to: x2) return x3 } return Self.init(fnc) } } public struct Map<Domain>: EndType { internal let fnc: (Domain) -> Domain public init(_ fnc: @escaping (Domain) -> Domain) { self.fnc = fnc } public func applied(to x: Domain) -> Domain { return fnc(x) } } public extension Array where Element: EndType { func composed() -> Element { return self.reduce(Element.identity) { (res, f) in res.composed(with: f) } } } let f: Map<Int> = Map { x in 2 * x } let fs: Array<Map<Int>> = [f, f, f] let g: Map<Int> = fs.composed() let t: Int = g.applied(to: 1) print(t)Swift version 4.1 (swift-4.1-RELEASE)8[omochi@omochi-iMac-PC43 et]$ swift a.swift 0 swift 0x00000001062c5ffa PrintStackTraceSignalHandler(void*) + 42 1 swift 0x00000001062c53b6 SignalHandler(int) + 966 2 libsystem_platform.dylib 0x00007fff55398f5a _sigtramp + 26 3 libsystem_platform.dylib 0x000000000000ffff _sigtramp + 2865197247 4 libsystem_platform.dylib 0x0000000109f2a4fb _sigtramp + 3032028603 5 libsystem_platform.dylib 0x0000000109f2a7c4 _sigtramp + 3032029316 6 libsystem_platform.dylib 0x0000000109f2a922 _sigtramp + 3032029666 7 libsystem_platform.dylib 0x0000000109f2ac61 _sigtramp + 3032030497 8 libswiftCore.dylib 0x000000010c0a894b _T0s8SequencePsE6reduceqd__qd___qd__qd___7ElementQztKctKlF + 603 9 libswiftCore.dylib 0x0000000109f2a8a7 _T0s8SequencePsE6reduceqd__qd___qd__qd___7ElementQztKctKlF + 425984863110 libswiftCore.dylib 0x0000000109f2a187 _T0s8SequencePsE6reduceqd__qd___qd__qd___7ElementQztKctKlF + 4259846807 11 swift 0x0000000103606c0a llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 362 12 swift 0x000000010360d30c llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, char const* const*) + 1004 13 swift 0x0000000102928735 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 48709 14 swift 0x000000010291ae64 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7908 15 swift 0x00000001028cf8b5 main + 18917 16 libdyld.dylib 0x00007fff5508a015 start + 1 17 libdyld.dylib 0x000000000000000a start + 2868338678 Stack dump: 0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret a.swift -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -color-diagnostics -module-name a Segmentation fault: 11// // main.swift // Math // // Created by tarunon on 2018/04/16. // Copyright © 2018年 tarunon. All rights reserved. // import Foundation public protocol MapType { associatedtype Domain associatedtype Codomain init(_ fnc: @escaping (Domain) -> Codomain) func applied(to x: Domain) -> Codomain } public struct Map<Domain, Codomain>: MapType { internal let fnc: (Domain) -> Codomain public init(_ fnc: @escaping (Domain) -> Codomain) { self.fnc = fnc } public func applied(to x: Domain) -> Codomain { return fnc(x) } } public protocol EndType: MapType where Domain == Codomain { static var identity: Self { get } func composed(with f: Self) -> Self } public extension EndType { public static var identity: Self { return _identity(of: Self.self) } public func composed(with g: Self) -> Self { return _composed(with: self, g) } } func _identity<E: EndType>(of type: E.Type = E.self) -> E { return E { x in x } } func _composed<E: EndType>(with f: E, _ g: E) -> E { return E { x in f.applied(to: g.applied(to: x)) } } extension Map: EndType where Domain == Codomain { } public extension Array where Element: EndType { func composed() -> Element { return self.reduce(_identity(of: Element.self)) { (res, f) in _composed(with: res, g: f) } } } let f = Map { x in 2 * x } let g = [f, f].composed() print(g.applied(to: 1)) (edited)import Foundation public protocol MapType { associatedtype Domain associatedtype Codomain init(_ fnc: @escaping (Domain) -> Codomain) func applied(to x: Domain) -> Codomain } public struct Map<Domain, Codomain>: MapType { internal let fnc: (Domain) -> Codomain public init(_ fnc: @escaping (Domain) -> Codomain) { self.fnc = fnc } public func applied(to x: Domain) -> Codomain { return fnc(x) } } public protocol EndType: MapType where Domain == Codomain { static var identity: Self { get } func composed(with f: Self) -> Self } public extension EndType { public static var identity: Self { return Self { x in x } } public func composed(with g: Self) -> Self { return Self { x in // ココで EXC_BAD_ACCESS self.applied(to: g.applied(to: x)) } } } extension Map: EndType where Domain == Codomain { public static var identity: Map<Domain, Codomain> { return Map { x in x } } public func composed(with g: Map<Domain, Codomain>) -> Map<Domain, Codomain> { print("Map.composed self: \(type(of: self))") return Map { x in exit(0) // return self.applied(to: g.applied(to: x)) } } } public extension Array where Element: EndType { func composed() -> Element { return self.reduce(Element.identity) { res, f in print("Array.composed res: \(type(of: res))") return res.composed(with: f) } } } let f = Map<Int, Int> { x in 2 * x } let g = [f, f].composed() print(g.applied(to: 0))Array.composed res: Map<Int, Int> Map.composed self: Map<Map<Int, Int>, Map<Int, Int>> Array.composed res: Map<Int, Int> Map.composed self: Map<Map<Int, Int>, Map<Int, Int>>res.composed の直前の res は Map<Int, Int> なのに、直後の composed の中の self は Map<Map<Int, Int>, Map<Int, Int>> になってる。protocol A { static func foo() } extension Dictionary: A where Key == Value { static func foo() { print(type(of: self)) } } extension Array where Element: A { static func foo() { Element.foo() } } [String: String].foo() [[String: String]].foo()Dictionary<String, String>.Type Dictionary<Dictionary<String, String>, Dictionary<String, String>>.Type==で束縛してConditional Conformanceを使い、それを他のGenerics ParameterのConditionに利用して関数を呼ぶとTypeが壊れる (edited)Usage: @swiftbot [--version=SWIFT_VERSION] [--command={swift, swiftc}] [--options=SWIFTC_OPTIONS] ``` [Swift Code] ``` Examples: @swiftbot ``` print("Hello world!") ``` @swiftbot --version=4.0.3 ``` print("Hello world!") ``` @swiftbot --command=swiftc --options=-dump-parse ``` print("Hello world!") ``` Subcommands: @swiftbot versions: show available Swift toolchain versions @swiftbot contribute: show repository URLs @swiftbot help: show helpimport Foundation public protocol MapType { associatedtype Domain associatedtype Codomain init(_ fnc: @escaping (Domain) -> Codomain) func applied(to x: Domain) -> Codomain } public struct Map<Domain, Codomain>: MapType { internal let fnc: (Domain) -> Codomain public init(_ fnc: @escaping (Domain) -> Codomain) { self.fnc = fnc } public func applied(to x: Domain) -> Codomain { return fnc(x) } } public protocol EndType: MapType where Domain == Codomain { static var identity: Self { get } func composed(with f: Self) -> Self } public extension EndType { public static var identity: Self { return Self{ x in x } } public func composed(with g: Self) -> Self { return Self { x in // ココで EXC_BAD_ACCESS self.applied(to: g.applied(to: x)) } } } extension Map: EndType where Domain == Codomain { } public extension Array where Element: EndType { func composed() -> Element { return self.reduce(Element.identity) { (res, f) in res.composed(with: f) } } } let f = Map { x in 2 * x } let g = [f, f].composed() print(g.applied(to: 0))Swift version 4.2-dev (LLVM d14a2b25f2, Clang c38020c511, Swift 22530b922f)/usr/bin/swift[0x41336e4] /usr/bin/swift[0x4133a26] /lib/x86_64-linux-gnu/libpthread.so.0(+0x11390)[0x7fd883d3e390] /usr/lib/swift/linux/libswiftCore.so(+0x42ea92)[0x7fd87eefca92] /usr/lib/swift/linux/libswiftCore.so(swift_getGenericMetadata+0x687)[0x7fd87eef6f87] [0x7fd88416d90a] [0x7fd88416d296] [0x7fd88416d35c] [0x7fd88416d41c] [0x7fd88416d65c] [0x7fd88416d715] [0x7fd88416d15e] /usr/bin/swift[0x10335de] /usr/bin/swift[0x1037702] /usr/bin/swift[0x4f21d2] /usr/bin/swift[0x4d9b77] /usr/bin/swift[0x4d503c] /usr/bin/swift[0x48a3ad] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fd882468830] /usr/bin/swift[0x487c09] Stack dump: 0. Program arguments: /usr/bin/swift -frontend -interpret /usercode/main.swift -disable-objc-interop -module-name main Segmentation fault (core dumped)CommandLine.arguments で配列取れませんでしたっけ?swift package generate-xcodeproj で作ったプロジェクトでフォルダの順番を入れ替えたりすると Xcode クラッシュしません? generate-xcodeproj で作ったばかりのプロジェクトでフォルダの順番を変えようとすると落ちます typealias でRename動かなかったり、 func test() { typealias Alias = String _ = Alias(12) // こっちをリネーム } (edited)realm-swift-3.5.0/examples/ios/swift-4.1 のディレクトリ内にあるProjectの設定がSwift3.3 らしいです。
dyld: Library not loadedの実行時エラーが出ます。$ carthage update --platform ios *** Fetching GPUImage *** Checking out GPUImage at "0.1.7" *** xcodebuild output can be found in /var/folders/62/nrp34pc96t550_hhml86hmwrr_q2rc/T/carthage-xcodebuild.MYLmCm.log *** Building scheme "GPUImageFramework" in GPUImage.xcodeproj Failed to write to /Users/snakagam/Desktop/MyLibrary/Carthage/Build/iOS/GPUImage.framework: Error Domain=NSCocoaErrorDomain Code=260 "The file “GPUImage.framework” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///Users/snakagam/Desktop/MyLibrary/Carthage/Checkouts/GPUImage/build/ArchiveIntermediates/GPUImageFramework/BuildProductsPath/Release-iphoneos/GPUImage.framework, NSFilePath=/Users/snakagam/Desktop/MyLibrary/Carthage/Checkouts/GPUImage/build/ArchiveIntermediates/GPUImageFramework/BuildProductsPath/Release-iphoneos/GPUImage.framework, NSUnderlyingError=0x7f870a806a90 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}swiftlintのバージョンを固定したい時に。 Carthageはバイナリの配布をサポートしていないのでCocoaPodsで管理する。 が、他のライブラリをCarthageで管理している場合、バイ...shtnkgm/GPUImageの0773513209ae9f066db82497fd2342e0241d4fa2は通りました*** Cloning GPUImage *** Checking out GPUImage at "0.1.10" *** xcodebuild output can be found in /var/folders/62/nrp34pc96t550_hhml86hmwrr_q2rc/T/carthage-xcodebuild.5AWMmm.log *** Building scheme "GPUImageFramework" in GPUImage.xcodeproj Failed to write to /Users/snakagam/Desktop/MyLibrary/Carthage/Build/iOS/GPUImage.framework: Error Domain=NSCocoaErrorDomain Code=260 "The file “GPUImage.framework” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///Users/snakagam/Desktop/MyLibrary/Carthage/Checkouts/GPUImage/framework/build/ArchiveIntermediates/GPUImageFramework/BuildProductsPath/Release-iphoneos/GPUImage.framework, NSFilePath=/Users/snakagam/Desktop/MyLibrary/Carthage/Checkouts/GPUImage/framework/build/ArchiveIntermediates/GPUImageFramework/BuildProductsPath/Release-iphoneos/GPUImage.framework, NSUnderlyingError=0x7fa56e662420 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}rm -rf ~/Library/Developer/Xcode/DerivedData/ //parametersを定義 var parameters: Parameters = [ "action":"query", "prop":"revisions", "rvprop":"content", "format":"json", "titles":"tennis" ] var urlString = "https://en.wikipedia.org/w/api.php" //WikipediaAPIを通信する func getWiki() { Alamofire.request(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default,headers:nil) .responseJSON { response in debugPrint(response) } } 以下が出力されたエラーの部分です [Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) [Timeline]: Timeline: { "Request Start Time": 548742968.775, "Initial Response Time": 548742970.085, "Request Completed Time": 548742970.088, "Serialization Completed Time": 548742970.106, "Latency": 1.309 secs, "Request Duration": 1.312 secs, "Serialization Duration": 0.018 secs, "Total Duration": 1.330 secs } Restlet client でAPIの動作自体を確認しましたが,上のパラメーターで無事にテニスに関する記事が取得できたので,おそらくAlamofireの使い方が間違っている可能性が高いと考えています. どなたかご教授いただけますと幸いです.どうぞよろしくお願いいたします. (edited)curl "https://en.wikipedia.org/w/api.php" -G -d action=query -d prop=revisions -d rvprop=content -d format=json -d titles=tennis > a.json cat a.json | jq[Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(...encoding:部分不要ぽいですね。 func getWiki() { Alamofire.request(urlString, method: .get, parameters: parameters) .responseJSON { response in debugPrint(response) } } これで正しくJSON返ってくるInit(...)という関数が見つからないというエラーです。Use of unresolved identifier Initなので、Initを呼び出しているけどどこにも見つからなかった、というエラーです。 ConfigurationBlock.swiftで定義されている関数なのでそのファイルを忘れてませんか? (edited)Init(value: になっているから、使う側も今のSwiftの文法なら Init(value: で呼び出す必要がありますね、昔Swift 2までは一つめの引数がデフォルトでラベルとして使わなかったので@noescape が怒られてるんじゃないか?@escaping ってクロージャ型の左側(コロンの右側)に書くけどclass ResCell: UITableViewCell { static var textViewLeftMargin: CGFloat { return 16 } } class ResCell2: ResCell { /// override static var textViewLeftMargin: CGFloat { return 8 } } これ書いたら,怒られた.static varはoverrideできんと.なんだろう,これはstaticは,継承によって挙動が左右されないことを保証するためにあるってことですかね 心は,こういうこと?class ResCell: UITableViewCell { class let textViewLeftMargin: CGFloat = 16 } classでもlet宣言はoverrideできないとかあるclass内のstaticはfinal classですよね。public protocol IntModuleObjectType: ModuleObjectType where R == 𝐙 { ... } extension ModuleObject: IntModuleObjectType where R == 𝐙 {} この 297 行目でコメントアウトしてる部分を外すと、 100 行目で Binary operator '==' cannot be applied to operands of type '_Matrix<Dynamic, Dynamic, R>' and '_Matrix<Dynamic, Dynamic, ModuleObject.R>' (aka '_Matrix<Dynamic, Dynamic, Int>') と勝手に R == Int と仮定されて怒られているようです deadline で指定された時間に main キューで execute のクロージャ式の中身を実行という意味ですね。 (edited)DispatchQueue についてちゃんと理解するためには↓あたりを読むと確実です。(もし並行処理について詳しくなければ Introduction から読んだ方が良いです。が、並行処理はそれだけで本が一冊書けるような分野なので、まずは日本語で並行処理について書かれた本を読んで概念を習得した方が良いかもしれません。) https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW11. While running pass #509 SILModuleTransform "Mandatory Inlining of Transparent Functions". <unknown>:0: error: unable to execute command: Abort trap: 6 <unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation) と出て通らなくなってしまいました [omochi@omochi-iMac-PC43 SwiftyMath (master=)]$ swift build Compile Swift Module 'SwiftyMath' (70 sources) Compile Swift Module 'SwiftyLieGroups' (12 sources) Compile Swift Module 'SwiftyHomology' (10 sources) Compile Swift Module 'SwiftyKnots' (9 sources) Compile Swift Module 'SwiftyTopology' (15 sources) Linking ./.build/x86_64-apple-macosx10.10/debug/libdSwiftyMath.dylib Compile Swift Module 'Sample' (1 sources) Linking ./.build/x86_64-apple-macosx10.10/debug/Sample [omochi@omochi-iMac-PC43 SwiftyMath (master=)]$ swift --version Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2) Target: x86_64-apple-darwin17.6.0var unions: Set<Set<Self>> = Set() ... return unions .sorted{ $0.count < $1.count } .map{ FiniteSubgroupStructure(allElements: $0) } (edited)swift-4.2-DEVELOPMENT-SNAPSHOT-2018-06-11-aだと $ docker run --privileged -it -v `pwd`:`pwd` -w `pwd` --rm norionomura/swift:swift-4.2-branch swift test Compile Swift Module 'SwiftyMath' (70 sources) /Users/norio/github/SwiftyMath/Sources/SwiftyMath/Matrix/SquareMatrix.swift:18:1: error: conditional conformance of type '_Matrix<n, m, R>' to protocol 'Ring' does not imply conformance to inherited protocol 'Monoid' extension SquareMatrix: Ring where n == m { ^ /Users/norio/github/SwiftyMath/Sources/SwiftyMath/Matrix/SquareMatrix.swift:18:1: note: did you mean to explicitly state the conformance like 'extension _Matrix: Monoid where ...'? extension SquareMatrix: Ring where n == m { ^ error: terminated(1): /usr/bin/swift-build-tool -f /Users/norio/github/SwiftyMath/.build/debug.yaml test output: になります。 (edited)昨日は普通にできてたんですが… クラッシュ再現しないです。 こちらもビルドできた。 Abort trap: 6ってことはコンパイラがクラッシュしてるので、コンパイラのバグを踏んだかもしれませんね コンパイラのメモリ関連バグ?マシン再起動したら直ったりしませんか? (edited)layoutMarginsGuide がありますcontentView があって、それはlayoutMarginsの中にあるじゃなかったっけ protocol HogeProtocol { } struct Hoge: HogeProtocol { func setHoge() {} } struct Fuga: HogeProtocol { func setFuga() {} } enum Foo { case hoge case fuga func getHogeProtocol() -> HogeProtocol { switch self { case .hoge: return Hoge() case .fuga: return Fuga() } } } // use let hoge = Foo.hoge.getHogeProtocol() // HogeProtocol型 hoge.setHoge() // NG
FooのgetHogeProtocol() の返り値で各structの型を返したいのですが、HogeProtocol型になってしまいます。 各structの型を返す方法はありますでしょうか。protocol HogeProtocol { } struct Hoge: HogeProtocol { func setHoge() {} } struct Fuga: HogeProtocol { func setFuga() {} } enum Foo { case hoge case fuga func getHogeProtocol<T : HogeProtocol>() -> T { switch self { case .hoge: return Hoge() as! T case .fuga: return Fuga() as! T } } } // use let hoge: Hoge = Foo.hoge.getHogeProtocol() // HogeProtocol型 hoge.setHoge() // OKprotocol HogeProtocol { } struct Hoge: HogeProtocol { func setHoge() {} } struct Fuga: HogeProtocol { func setFuga() {} } enum Foo { case hoge case fuga func getHogeProtocol<T : HogeProtocol>() -> T { switch self { case .hoge: return Hoge() as! T case .fuga: return Fuga() as! T } } } // use let hoge: Hoge = Foo.hoge.getHogeProtocol() // HogeProtocol型 hoge.setHoge() // OKswift-4.1.1-RELEASEfunc getHogeProtocol<T : HogeProtocol>() -> T? にして as? T にすべきだとは思いますが。protocol HogeProtocol { } struct Hoge: HogeProtocol { func setHoge() {} } struct Fuga: HogeProtocol { func setFuga() {} } enum Foo { case hoge case fuga func getHogeProtocol() -> FooHogeProtocolValue { switch self { case .hoge: return FooHogeProtocolValue.hoge(Hoge()) case .fuga: return FooHogeProtocolValue.fuga(Fuga()) } } } enum FooHogeProtocolValue { case hoge(Hoge) case fuga(Fuga) }
@shunkun こうするのはどうですかswitch foo.getHogeProtocol() { case .hoge(let hoge): // なにかする case .fuga(let fuga): // なにかする } これで型安全ですenum Foo { case hoge case fuga } で、どっちかを最初に選んで使うので、選んだ後に場合分けが入ってしまうと使うの側のコードが増えてしまうなと言う印象なのです... (edited)enum FooHogeProtocolValue を HogeProtocol に準拠させれば、分岐消せますね。 (edited)let hoge: Fuga = Foo.hoge.getHogeProtocol() だとクラッシュですね...unowned let hoge: Hoge という宣言をしているコードを見かけたのですが、これってどういう挙動のものなんでしょうか そのクラスにはインスタンスは保持されずに他で保持されてなければhogeは即解放されてしまう…?weak の場合は解放されたときに nil にしてくれますが、 unowned の場合はそういう安全機構がありません。SIGABRT吐いちゃいますね…。 これがweakの場合はnilになっているとUse an unowned reference only when you are sure that the reference always refers to an instance that has not been deallocated. If you try to access the value of an unowned reference after that instance has been deallocated, you’ll get a runtime error.unowned(unsafe) の場合は未定義ですね。普通の unowned はドキュメントの通り、実行時エラーが保証されています。-Ounchecked のときは普通の unowned でも未定義に・・・。 $ cat unowned.swift class A {} class B { unowned let a: A init(a: A) { self.a = a } } let b = B(a: A()) print(b.a) $ swift -Ounchecked unowned.swift unowned.A (edited)unowned(unsafe) の有用性を認めるのであれば、 unsafePrecondition とかも認めないといけないような-Ounchecked でコントロールスタイルじゃないのかな・・・。-Ounchecked 自体deprecatedじゃないかな。。。-Ounchecked を使わないと、十分に安全性が確保されたパフォーマンスを追求するコードで一体どうやって precondition のチェックを無効化したらいいんでしょう?assert で書く?private プロパティなどを Codable の対象から外す簡単な方法ってありましたっけ?Codable でない型にしたら encode init(from:) を自前実装しないといけないのでは?CodingKeys だけ書いたら encode と init(from:) は自動生成されるんだっけ?
(edited)struct Foo: Codable { private enum CodingKeys: CodingKey { case a } var a: Int private var _b: String }struct Foo: Codable { private enum CodingKeys: CodingKey { case a } var a: Int private var _b: String }swift-4.1.1-RELEASE/usercode/main.swift:1:8: error: type 'Foo' does not conform to protocol 'Decodable' struct Foo: Codable { ^ Swift.Decodable:2:12: note: protocol requires initializer 'init(from:)' with type 'Decodable' public init(from decoder: Decoder) throws ^ /usercode/main.swift:6:17: note: cannot automatically synthesize 'Decodable' because '_b' does not have a matching CodingKey and does not have a default value private var _b: String ^encode, decode を自前実装しないといけないのか・・・。面倒だなぁ。-O なら assetionFailure は有効にならないはずです。-O (Optimize for Speed)になってるんですよね。-O による assertion の除去がされているか確認するために、 おしたら assertionFailure するだけのボタンをアプリに置いてみるなどはどうでしょう。import Foundation var array = Array(repeating: 1, count: 100000) DispatchQueue.global().async { array.insert(3, at: 99999) print("inserted") } print("start") let total = array.reduce(0, +) print(total) print(array.count)start inserted 100003 100001import Foundation var array = Array(repeating: 1, count: 100000) DispatchQueue.global().async { array.insert(3, at: 99999) print("inserted") } print("start") let total = array.reduce(0, +) print(total) print(array.count)start inserted 100000 100001dispatchMain()とかで待たないと、実行されない場合がありますよ。let total = ... の行については、そのキューの中では実行されてないです。Array の API リファレンスも The Swift Programming Language にも書かれてなさそう( thread で検索してみただけだけど)。標準ライブラリのコードから実態としてスレッドセーフじゃないということでしょうか?
(edited)Array自体はスレッドセーフなのでは?import Foundation let semaphore = DispatchSemaphore(value: 0) var array = [Int]() DispatchQueue.global().async { for i in 0...1000 { array.append(i) } print("end 1") } DispatchQueue.global().async { for i in 0...1000 { array.append(i) } print("end 2") } DispatchQueue.global().async { for i in 0...1000 { array.append(i) } print("end 3") } semaphore.wait()import Foundation var array = Array(repeating: 1, count: 100000) DispatchQueue.global().async { array.insert(3, at: 99999) print("inserted") } print("start") let total = array.reduce(0) { t, c in if t % 100 == 0 { print(".", terminator: "") } return t + c } print("") print(total) print(array.count)start ....................................inserted .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... 100000 100001すみません Swift42 bot 落としちゃったかも 受け取ったコードは別プロセスで実行されるので、ボット自体が落ちることはあまりないです。受け取ったコードは別プロセスで実行されるので、ボット自体が落ちることはあまりないです。 あいや落ちてHerokuが復帰させてた… var supportedPositions でカスタマイズできそうな雰囲気ですが、それはダメでしたか?
1insetFor(position:) で返す値を .tip: 0, .half: 100, .full: 400
initialPosition を .tip とし コードで表示・非表示を行う場合は func show() { fpc.move(to: .half) }
func hide() { fpc.move(to: .tip) } とすれば、それっぽい動きになりそうですね。 (このやり方だと隠れていても FloatingPanel 自体は存在する形になってしまいますが)let a = [1] let b = [1] if a == b {} // コンパイル通る if [1] == [1] {} // コンパイル通らない このように配列を比較したときに、 一旦定数に入れたものと、そうでないものとで結果が違うのは何故でしょうか??a, b は型が Array<Int> に確定しているので、 if a == b {} は問題ないのですが、 リテラルを直接書いた場合( if [1] == [1] {} )は、 演算子 == リテラルをどの型として評価すべきか1つに定まらないから、ですね。 ちなみに問題は == に配列リテラルを渡したときに、配列リテラルからインスタンスできる型が複数あるからですね。(IndexzSetとIndexPath?) 例えばFoundationをImportしなければそれは存在しないので、上記のコードもコンパイルできます。 (edited)どの型として評価すべきか1つに定まらないが 他の型ではArrayに定まるからなんですねif [1] as IndexPath == [1] {} このように左側だけキャストしてもコンパイルが通るのは、 勝手に右側も合わせてくれるからなのですか??== は static func == (Self, Self) -> Bool なので、どちらか一方が決まるともう片方も決まることになります。import SomeFramework public typealias Dog = SomeFramework.Dog public typealias Cat = SomeFramework.Cat@_exported import SomeFramework という記述ができますよ、非公式仕様ですが$ swift test -c release [3/3] Linking ./.build/x86_64-apple-macosx/release/memory-crash-testPackageTe… Test Suite 'All tests' started at 2019-03-29 18:38:34.556 Test Suite 'memory-crash-testPackageTests.xctest' started at 2019-03-29 18:38:34.556 Test Suite 'memory_crash_testTests' started at 2019-03-29 18:38:34.556 Test Case '-[memory_crash_testTests.memory_crash_testTests testExample]' started. broadcast: start broadcast: end will exit testExample Exited with signal code 11 誰かなにか分かりませんでしょうか? 環境 $ swift --version Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3) Target: x86_64-apple-darwin18.5.0 $ swift package --version Apple Swift Package Manager - Swift 5.0.0 (swiftpm-14490.60.2)swift-DEVELOPMENT-SNAPSHOT-2019-03-26-aでは直ってるみたいですね。swift-5.0-branchでは直ってないので、放っておくと5.1まで直らないのでは。ParameterというprotocolがRoutingの中で宣言されています。 Vaporというpackageの中で先日お聞きしたような@_exportedやtypealiasは使われていなかったのですが、MyProjectで import Vaporとしただけで、Parameterという名前を使えるのが不思議です。 SwiftPMで依存しているライブラリの名前は全て展開されるということなのでしょうか? https://github.com/vapor/routing.git
https://github.com/vapor/vapor.git @_exported import CryptoKit @_exported import HTTPKit @_exported import RoutingKit @_exported import ServiceKit @_exported import ConsoleKit @_exported import Foundation@_exported で検索すると多分アットマークがユーザー名の意味とかになってしまいますね、exportedで検索したら出ました。UIKit.UITableView などのようにSubmodule化することはできるのでしょうか? http://nsomar.com/modular-framework-creating-and-using-them/ この記事を読んでみたのですが、理解できていません。 (modulemapというのはObjective-Cなどで書かれたFrameworkの内部を切り分けるために使われるものでSwiftオンリーで書かれたものにはあまり関係ないのかなと朧げに理解しています) (edited)MyFramework.Sub1.Hoge
MyFramework.Sub2.Hogeimport Sub1 import Sub2 public typealias Sub1 = Sub1 public typealias Sub2 = Sub2 みたいに書いたり import Sub1 import Sub2 public struct Sub1 { public typealias Hoge = Sub1.Hoge } public struct Sub2 { public typealias Hoge = Sub2.Hoge } みたいに書いたりできないかなーと模索しているのですが。structの名前とsub frameworkの名前をずらせばこれでも書けない事はないのですが、全部aliasを貼るのもすごく面倒なのでもっといいやり方があるのではないかと思っています。import MyFramework fileprivate typealias Hoge = Sub1.Hoge とかやるしか無いと思います。 それかパッケージをわけてMyFrameworkSub1とMyFrameworkSub2を作る。 その上でMyFrameworkも作って、MyFrameworkSub1とMyFrameworkSub2をexportすれば、 ユーザ側で import MyFrameworkSub1 とできるようになります。import class UIKit.UITableViewController
https://thoughtbot.com/blog/swift-importsMyFrameworkのなかで以下のようにしておけば @_exported import Sub1 @_exported import Sub2 MyFrameworkのユーザー側で下記のようにSub1, Sub2を使えるのですね。 let hoge1 = Sub1.Hoge() let hoge2 = Sub2.Hoge() 名前の衝突が問題だったので、やりたかった事はこれで解決できました。ありがとうございます。 (@_exported importをすると、Sub1やSub2などの名前空間は消失してしまうのかと思っていました。)@_exported ってそんなに便利なものじゃないのでこの場合だと MyFramework と Sub1、Sub2をそれぞれ個別に、あるいはSub1、Sub2のフレームワークとして提供する方がいいと思いますよ。DispatchQueue.main.async(execute workItem: DispatchWorkItem)を呼んだ場合のworkItemの実行タイミングはどのように決まるのでしょうか? DispatchQueue.main.asyncをメインスレッドでworkItemを実行する程度にしか考えていなかったので // on main thread print("1") DispatchQueue.main.async { print("2") } print("3") のようなコードの実行順序が1,3,2になることに気がつけずにハマってしまい気になりました。Promise の then が resolve 済でも同期的に実行されない仕様なことに気づかずハマったことがあります。 console.log("1"); new Promise(resolve => { resolve(42); }).then(value => { console.log("2"); }); console.log("3");
1 3 2 (edited)async/await は async -> Foo でも同期であり得るという認識。 (edited)非同期コールバックを同期的に呼び出してはいけない の部分は、わかりやすくていいなと思っているんですが、ただし多用すると遅くなる public protocol AppConfigProvider: NSObject { var config: AppConfig { get set } } // MARK : - Rx properties extension Reactive where Base: AppConfigProvider { /// Reactive wrapper for `config` property. public var config: Observable<AppConfig?> { return base.rx.observe(AppConfig.self, "config") } }var environment: AppConfigProvider// Type Erasure public class AnyAppConfigProvider : NSObject, AppConfigProvider { public var config: AppConfig { get { return base.config } set { base.config = newValue } } private let base: AppConfigProvider public init<X: AppConfigProvider>(_ base: X) { self.base = base } } // 実際の具体的なAppConfigProvider public class ACP : NSObject, AppConfigProvider { public var config: AppConfig public init(config: AppConfig) { self.config = config } } // 利用時 var a: AnyAppConfigProvider = AnyAppConfigProvider(ACP(config: AppConfig())) print(a.rx.config) (edited)AppConfigProvider で受け取るのではなくて、 AnyAppConfigProvider で受け取るようにします。 代入するときは AnyAppConfigProvider.init<X> で明示的に包み込みます。RxSwift.ReactiveCompatible の var rx: Reactive<Self> のところで、 プロトコル型それ自身を Self として使おうとしているからです。
public protocol AppConfigProvider { var config: AppConfig { get set } var rx_config: Observable<AppConfig> { get } } public class ACP : AppConfigProvider { public var configSubject: PublishSubject<AppConfig> public var config: AppConfig { return try! configSubject.value()} public var rx_config: Observable<AppConfig> { return configSubject } }a.config
a.rx.config みたいに分けたいのですが、難しいですかね・・・?struct Reactive<Base> {} protocol ReactiveExtensionCompatible { associatedtype Target var rx: Reactive<Target> { get } } extension ReactiveExtensionCompatible { var rx: Reactive<Self> { fatalError() } } struct Observable<T> {} import Foundation extension NSObject: ReactiveExtensionCompatible {} // ここまでRx struct AppConfig {} protocol AppConfigProvider: NSObject { var config: AppConfig { get set } } extension AppConfigProvider { var rx: Reactive<AppConfigProvider> { fatalError() } } extension Reactive where Base == AppConfigProvider { var config: Observable<AppConfig?> { fatalError() } } func useConfig(_ provider: AppConfigProvider) { _ = provider.rx.config } (edited)rx のオーバーロード定義の fatalError() のところは実際には return Reactive(self) とする必要ありですか? (edited)struct Reactive<Base> {} protocol ReactiveExtensionCompatible { associatedtype Target var rx: Reactive<Target> { get } } extension ReactiveExtensionCompatible { var rx: Reactive<Self> { fatalError() } } struct Observable<T> {} import Foundation extension NSObject: ReactiveExtensionCompatible {} // ここまでRx struct AppConfig {} protocol AppConfigProvider: NSObject { var config: AppConfig { get set } } extension AppConfigProvider { var rx: Reactive<AppConfigProvider> { fatalError() } } extension Reactive where Base: AppConfigProvider { var config: Observable<AppConfig?> { fatalError() } } func useConfig(_ provider: AppConfigProvider) { _ = provider.rx.config }<stdin>:39:21: error: protocol type 'AppConfigProvider' cannot conform to 'AppConfigProvider' because only concrete types can conform to protocols _ = provider.rx.config ^public protocol AppConfigProvider: NSObject { var config: AppConfig { get set } } extension AppConfigProvider { var rx: Reactive<AppConfigProvider> { return Reactive(self) } } extension Reactive where Base: AppConfigProvider { var config: Observable<AppConfig?> { return base.rx.observe(AppConfig.self, "config") } }
a.rx.configを呼ぼうとすると下記のエラーが出てしまいました。 KVC-compliantにするためには何が必要なのでしょうか? Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<App.AppEnvironment 0x282777d20> addObserver:<_TtC7RxCocoaP33_F7515DBB13B60709A3CB25DD19EDD11D11KVOObserver 0x280d28690> forKeyPath:@"config" options:5 context:0x0] was sent to an object that is not KVC-compliant for the "config" property.'extension Reactive whare Base == AppConfigProvider にしていないから、KeyPath版の方にfallしたんじゃないですかね。== にしてみたらどうでしょう?Ambiguous reference to member 'observe(_:_:options:retainSelf:)'@objc can only be used with members of classes, @objc protocols, and concrete extensions of classespublic protocol AppConfigProvider: NSObject { @objc var config: AppConfig { get set } var rx_config: Observable<AppConfig> { get } }Property cannot be a member of an @objc protocol because its type cannot be represented in Objective-Cや Protocol 'AppConfigProvider' is '@objc' and cannot have a superclass constraint が出てしまいます。 public @objc protocol AppConfigProvider: NSObject { var config: AppConfig { get set } var rx_config: Observable<AppConfig> { get } }observe を使いたい場合は (base as NSObject).rx.~~~ でできると思います
public final class AppConfig: NSObject { public init() {} } @objc public protocol AppConfigProvider: NSObjectProtocol { @objc var config: AppConfig { get set } } extension AppConfigProvider { var rxoriginal: Reactive<AppConfigProvider> { return Reactive(self) } } extension Reactive where Base: AppConfigProvider { var config: Observable<AppConfig?> { return (base as! NSObject).rx.observe(AppConfig.self, "config") } }protocol AppConfigProvider { var rx: RxEnvironmentProvider { get } var config: AppConfig { get set } } protocol RxAppConfigProvider { var config: Observable<AppConfig> { get } } final class AppEnvironment: AppConfigProvider { private var rx: RxAppEnvironment var config: AppConfig { get { return try! configSubject.value() } set { rx.configSubject.onNext(newValue) } } } struct RxAppEnvironment: RxAppConfigProvider { let configSubject: BehaviorSubject<Entity.AppConfig> var config: Observable<AppConfig> { return configSubject } }protocol Foo { associatedtype Bar func hoge() } extension Foo where Bar: Decodable { func hoge() { print("Decodable.hoge") } } extension Foo where Bar == String { func hoge() { print("String.hoge") } } struct FooImpl1: Foo { typealias Bar = [String] } struct FooImpl2: Foo { typealias Bar = String } let foo1: FooImpl1 = FooImpl1() let foo2: FooImpl2 = FooImpl2() foo1.hoge() foo2.hoge()Decodable.hoge String.hoge==のほうが強い、みたいな規則がありますimport Foundation protocol Foo { associatedtype Bar func hoge() } extension Foo where Bar: Decodable { func hoge() { print("Decodable.hoge") } } extension Foo where Bar: Encodable { func hoge() { print("Encodable.hoge") } } struct FooImpl1: Foo { typealias Bar = String } struct FooImpl2: Foo { typealias Bar = String } let foo1: FooImpl1 = FooImpl1() let foo2: FooImpl2 = FooImpl2() foo1.hoge() foo2.hoge()<stdin>:20:8: error: type 'FooImpl1' does not conform to protocol 'Foo' struct FooImpl1: Foo { ^ <stdin>:5:10: note: multiple matching functions named 'hoge()' with type '() -> ()' func hoge() ^ <stdin>:9:10: note: candidate exactly matches func hoge() { ^ <stdin>:15:10: note: candidate exactly matches func hoge() { ^ <stdin>:24:8: error: type 'FooImpl2' does not conform to protocol 'Foo' struct FooImpl2: Foo { ^ <stdin>:5:10: note: multiple matching functions named 'hoge()' with type '() -> ()' func hoge() ^ <stdin>:9:10: note: candidate exactly matches func hoge() { ^ <stdin>:15:10: note: candidate exactly matches func hoge() { ^ <stdin>:31:1: error: ambiguous use of 'hoge()' foo1.hoge() ^ <stdin>:9:10: note: found this candidate func hoge() { ^ <stdin>:15:10: note: found this candidate func hoge() { ^ <stdin>:32:1: error: ambiguous use of 'hoge()' foo2.hoge() ^ <stdin>:9:10: note: found this candidate func hoge() { ^ <stdin>:15:10: note: found this candidate func hoge() { ^import Foundation protocol Foo { associatedtype Bar func hoge() } extension Foo where Bar: Decodable { func hoge() { print("Decodable.hoge") } } extension Foo where Bar: Encodable { func hoge() { print("Encodable.hoge") } } struct DecodableBar: Decodable {} struct EncodableBar: Encodable {} struct FooImpl1: Foo { typealias Bar = DecodableBar } struct FooImpl2: Foo { typealias Bar = EncodableBar } let foo1: FooImpl1 = FooImpl1() let foo2: FooImpl2 = FooImpl2() foo1.hoge() foo2.hoge()Decodable.hoge Encodable.hogeNo such module と言われてしまい、対処法がわかりません。 App : アプリ本体のFramework。他の全てのFrameworkに依存。 Feature1 : ある機能の画面やロジックなどが入ったFramework Feature2 : ある機能の画面やロジックなどが入ったFramework AppCore : AppやFeature1、Feature2などが依存するFramework こんな感じの構成でして、 AppからはCPのframeworkを使用できるのですが、その他のFrameworkでは使えなくて困っています。workspace 'App.xcworkspace' project 'Sources/App/App.xcodeproj' target 'App' do use_frameworks! pod 'Firebase/Core' pod 'Fabric', '~> 1.10.0' pod 'Crashlytics', '~> 3.13.0' target 'AppTests' do inherit! :search_paths end endworkspace 'App.xcworkspace' target 'App' do project 'Sources/App/App.xcodeproj' use_frameworks! install_pods target 'AppTests' do inherit! :search_paths end end target 'Feature1' do project 'Sources/Features/Feature1/Feature1.xcodeproj' install_pods target 'Feature1Tests' do inherit! :search_paths end end def install_pods use_frameworks! pod 'Firebase/Core' pod 'Fabric', '~> 1.10.0' pod 'Crashlytics', '~> 3.13.0' endwindow.rootViewController = vc とかをやっていると、present(nextView) で表示してますか? (edited)dismissを呼び出す対象のオブジェクトが間違ってるかもしれません。self.navigationController?.dismiss() を呼ぶとどうなるんですか?self.navigationController?.dismiss() の self がDelete sc.と表現されているViewControllerなら、Cover sc.のViewControllerまでは戻りそうな気がぼくもしてたんですけど。self.navigationController?.presentingViewController?.presentingViewController?.dismiss(animated: true)self.navigationController?.presentingViewController?.presentingViewController?.presentingViewController?.dismiss(animated: true)AppDelegateのプロパティとかにloginVCをプロパティで持っておいて(UIApplication.shared.delegate as! AppDelegate).loginVC?.dismiss(animated: true)class ViewController : UIViewController { @IBOutlet var textField: UITextField! let keyboardCloseHelper = KeyboardCloseHelper() override func viewDidLoad() { super.viewDidLoad() keyboardCloseHelper.add(textField) } }
self.view.endEditing(true) でそのビューに乗っているビュー全部に対して resignFirstResponder を呼んでくれるので、それを適当なタイミングで呼んだらいいと思いますよ。// swift-tools-version:5.0buttonItemsStatus の先頭あたりでいったん titleTextField.text と detailTextView.text の値を、それぞれ別の変数に入れてみてデバッガで止めるなり、ログに出すなりしてその値がどうなってるか見てみるとかしますね。buttonItemsStatus が呼ばれているだろうか?というあたりですかね。debugQuickLookObject生やすとXcode上でプレビューを表示する機能ありますけど これってstructでも同様にプレビュー表示したい場合なにか対応方法あるでしょうか? classに生やす場合@objc func debugQuickLookObject() -> AnyObject?で定義するため、structだとそもそも宣言できません。- (id)debugQuickLookObject メソッドで実現されてるので無理そうじゃない?@_specialize で複数の型パラメータを特殊化するのってできないでしょうか? 行列の計算を @_specialize(where R == Int, Double) のようにして、頻用されるものについては特殊化して高速化したいのが目的です。@_specialize(exported: true, kind: full, where K == Int, V == Int) @_specialize(exported: false, kind: partial, where K: _Trivial64) func dictFunction<K, V>(dict: Dictionary<K, V>) { }.pointeeにアクセスしてよいのは「初期化済み」のときだけです。label.numberOfLines = 1 にしてからもう一回 sizeToFit() してみましょうか? sizeToFit() はラベルのサイズを動的に調節するメソッドではなく、ラベルのサイズを呼び出された時点のコンテンツに合わせて調節するメソッドですsizeThatFits(_:) メソッドは親ビューにこのメソッド呼ばれた時の返してあげたいサイズを返すメソッドですので、それ呼ばれても特にサイズの変更はありませんleave のとこで EXC_BAD_INSTRUCTION が出てしまいます。count を 1 にすると通るのですが。 var a = Array(repeating: 0, count: 10) let group = DispatchGroup() group.enter() for i in a.indices { DispatchQueue.global().async { a[i] = i group.leave() // EXC_BAD_INSTRUCTION } } group.wait() print(a) // [0, 1, ..., 9] と出て欲しい (edited)var a = Array(repeating: 0, count: 10) let q = DispatchQueue(label: "array") let group = DispatchGroup() group.enter() for _i in a.indices { let i = _i DispatchQueue.global().async { q.sync { a[i] = i } group.leave() // EXC_BAD_INSTRUCTION } } group.wait() print(a) // [0, 1, ..., 9] と出て欲しい_iに置いてみましたvar a = Array(repeating: 0, count: 10) let q = DispatchQueue(label: "array") DispatchQueue.concurrentPerform(iterations: 10) { i in q.sync { a[i] = i } } print(a) // [0, 1, ..., 9] と出て欲しい (edited)var a = Array(repeating: 0, count: 10) let b = Array(0 ..< 10) let q = DispatchQueue(label: "array") DispatchQueue.concurrentPerform(iterations: 10) { i in let x = b[i] * 2 q.sync { a[i] = x } } (edited)let x = ... のところに、重い処理が入る予定。parallelMap実装を1年以上使ってます。 https://github.com/realm/SwiftLint/blob/740e398f9115f1f16048ea6979060f4e83a36b1c/Source/SwiftLintFramework/Extensions/Array%2BSwiftLint.swift#L56-L64 (edited)enum Enum: Equatable { case a case b(Int) } let a = Enum.a print(a == Enum.a) let b = Enum.b(1) //print(b is Enum.b)??? assocvalue付きのケースについてある変数があてはまるか確認する式ってないんですっけ? assertに書こうとしたら詰まったんですが。 (edited)enum Enum: Equatable { case a case b(Int) } let a = Enum.a print(a == Enum.a) let b = Enum.b(1) print(b == Enum.b(2))enum Enum: Equatable { case a case b(Int) } let a = Enum.a print(a == Enum.a) let b = Enum.b(1) print(b == Enum.b(2))swift-4.2.1-RELEASEtrue falselayoutSubviews で label の frame を更新するか、 AutoLayout を使うといいですよclass PlaceHolderTextView: UITextView { override func layoutSubviews() { super.layoutSubviews() placeHolderLabel.frame = bounds.insetBy(dx: 6, dy: 6) } }layoutSubviewsでも問題は解決できます...)CGRect(x: 0, y: 0, width: 100, height: 50).insetBy(dx: 5, dy: 5) とすれば {x 5 y 5 w 90 h 40}と内側に dx, dy分インセットした領域を返す関数です
createLabelのoriginを持ってきたからですfirstBaselineなど駆使する感じですねplaceholderLabel.sizeToFit()を付け足せばうまくいきそうですstrongly discourage (非常に非推奨)だけですのでOne-click with Heroku とあるので 下記リンクのDeploy to HerokuをクリックするだけでHeroku側の設定は完了するかと。 https://github.com/bih/spotify-token-swap-service#one-click-with-herokuClient ID と Client Secret をアプリに持たせると解読等された場合に悪用される可能性あるから、 Client Secret はサーバ側に持たせることで、サーバ経由のToken更新を行い、セキュアな状態にするためみたいDeploy to Herokuをクリック 2. https://github.com/bih/spotify-token-swap-service#configuration の環境変数項目のRequiredを最低限設定する --- /api/token や /api/refresh_token はHerokuサーバに対して行います。 つまり、Heroku設定後、quick-startの続きにあるコードの [my token swap app domain] をherokuのアドレスに置き換えてiOSアプリを実装したらOKです。 仮にHerokuサーバのアドレスが https://swap.heroku.com/ であれば… // Swiftコード lazy var sessionManager: SPTSessionManager = { if let tokenSwapURL = URL(string: "https://swap.heroku.com/api/token"), let tokenRefreshURL = URL(string: "https://swap.heroku.com/api/refresh_token") { self.configuration.tokenSwapURL = tokenSwapURL self.configuration.tokenRefreshURL = tokenRefreshURL self.configuration.playURI = "" } let manager = SPTSessionManager(configuration: self.configuration, delegate: self) return manager }() のように書いて、quick-startを進めていけば大丈夫かと思います
(edited)protocol X { static func hello() } typealias IntArray = [Int] extension IntArray: X { // typealias した型に対して extension static func hello() { print("hello") } } [Int].hello() // OK [String].hello() // ちゃんとエラーになる: Referencing static method 'hello()' on 'Array' requires the types 'String' and 'Int' be equivalent (edited)typealias GridChainComplex = ChainComplex1<FreeModule<TensorGenerator<MonomialGenerator<_Un>, GridDiagram.Generator>, 𝐙₂>> extension GridChainComplex { // こう書きたい }public typealias Single<Element> = PrimitiveSequence<SingleTrait, Element>protocol Helloable { static func hello() } struct Something<A, B> {} typealias Something_Int<B> = Something<Int, B> extension Something_Int: Helloable where B == Int { static func hello() { print("hello") } } Something<Int, Int>.hello() // OK Something_Int<Int>.hello() // OK Something_Int<String>.hello() // NG Python.import って動かないですか? 1> import Python 2> let numpy = Python.import("numpy") error: repl.swift:2:13: error: module 'Python' has no member named 'import' let numpy = Python.import("numpy") ^~~~~~ ~~~~~~ (edited)swift -frontend -replで起動して、インポートして:print_module <モジュール名>ですね。xcode-selectでXcode-beta.appを選択していない場合、-sdkパラメータも追加で渡す必要があるぽい。$ DEVELOPER_DIR=/Applications/Xcode-beta.app swift -frontend -repl -sdk $(DEVELOPER_DIR=/Applications/Xcode-beta.app xcrun -sdk macosx -show-sdk-path) *** You are running Swift's integrated REPL, *** *** intended for compiler and stdlib *** *** development and testing purposes only. *** *** The full REPL is built as part of LLDB. *** *** Type ':help' for assistance. *** (swift) (edited):print_module Pythonしたら4000行ぐらい出てきた@_exported import Python.import_ これは見えるけど import_は無いなあfile:///Users/otoyamisato/Mydictionary/MyDictionary/Base.lproj/Main.storyboard: error: IB Designables: Failed to render and update auto layout status for NewNoteViewController (iIJ-cw-XLr): Failed to launch designables agent. Check the console for a more detailed description and please file a bug report at bugreport.apple.com. (edited)allowsMultipleSelectionDuringEditingメソッドで、タップをしたらチェックマークがつくのって、どういう仕組みなのでしょうか? https://developer.apple.com/documentation/uikit/uitableview/1614944-allowsmultipleselectionduringedi (edited)$ swift build --verbose lsb_release -r /home/taketo1024/swift-5.0.2-RELEASE-ubuntu18.04/usr/bin/swiftc --driver-mode=swift -L /home/taketo1024/swift-5.0.2-RELEASE-ubuntu18.04/usr/lib/swift/pm/4_2 -lPackageDescription -swift-version 5 -I /home/taketo1024/swift-5.0.2-RELEASE-ubuntu18.04/usr/lib/swift/pm/4_2 -sdk / /home/taketo1024/ClosedRegion/Package.swift -fileno 7 which clang /home/taketo1024/swift-5.0.2-RELEASE-ubuntu18.04/usr/bin/clang -target x86_64-unknown-linux --sysroot / -fPIC -g -O0 -DSWIFT_PACKAGE=1 -DDEBUG=1 -fblocks -fmodules -fmodule-name=LinBoxWrapper -I /home/taketo1024/ClosedRegion/Sources/LinBoxWrapper/include -fmodules-cache-path=/home/taketo1024/ClosedRegion/.build/x86_64-unknown-linux/debug/ModuleCache -framework Accelerate -MD -MT dependencies -MF /home/taketo1024/ClosedRegion/.build/x86_64-unknown-linux/debug/LinBoxWrapper.build/LinBoxWrapper.mm.d -c /home/taketo1024/ClosedRegion/Sources/LinBoxWrapper/LinBoxWrapper.mm -o /home/taketo1024/ClosedRegion/.build/x86_64-unknown-linux/debug/LinBoxWrapper.build/LinBoxWrapper.mm.o : . fatal error: 'Foundation/Foundation.h' file not found #import <Foundation/Foundation.h> ^~~~~~~~~~~~~~~~~~~~~~~~~ と出ました。Obj-C をビルドするのに何かがいるのかな sudo apt-get install gnustep gnustep-devel gobjc で /usr/include/GNUstep/ 下に Foundation/ が入りましたが,これはどうやって SwiftPM で指定するのかしら"-I", " /usr/include/GNUstep" をcxxSettingsに追加したらどうなります?In file included from /home/taketo1024/ClosedRegion/Sources/LinBoxWrapper/LinBoxWrapper.mm:8: In file included from /home/taketo1024/ClosedRegion/Sources/LinBoxWrapper/include/LinBoxWrapper.h:8: In file included from /usr/include/GNUstep/Foundation/Foundation.h:30: /usr/include/GNUstep/GNUstepBase/GSVersionMacros.h:361:16: fatal error: 'objc/blocks_runtime.h' file not found # include <objc/blocks_runtime.h> ^~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.extern "C" { } で包むのとextern "C" { } は C++の中で使うC言語対応させる文法なので#ifdef __cplusplus extern "C" { #endif // ここに Swift に expose する struct や関数を書く #ifdef __cplusplus } #endif#ifdef __cplusplus extern "C" { #endif void hehe(); #ifdef __cplusplus } #endif hoge.cpp #include "hoge.h" void hehe() { std::cout << "hehe" << std::endl; } これが動いたのであとは頑張れば行けそうですbool solve(size_t rows, size_t cols, const LBMatrixComponent *Acomps, size_t Acomps_cnt, const LBMatrixComponent *bcomps, size_t bcomps_cnt, LBMatrixComponent *xcomps, size_t *xcomps_cnt); 生々 C_Nullable, _Nonnullとか使えるので、その辺りをちゃんと使うとSwift側でのポインタの扱いを簡単に出来たりします。const Hoge *const *xcomps が Swift では UnsafePointer<UnsafePointer<Hoge>?>! となるようで、この内側の ? を取りたくて const Hoge *const _Nonnull *xcomps としたら求める形にできたのですが,Pointer is missing a nullability type specifier という警告がたくさん出てしまいました。 NS_ASSUME_NONNULL_BEGIN ... END で囲めば消せるようなのですが、これを使うには Foundation を import する必要があるらしいんですが、それ以外の方法はあるでしょうか?(Linux で動かしたいので Foundation free にしたい)#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wnullability-completeness" ... #pragma clang diagnostic pop これで行けました#if __has_feature(assume_nonnull) #define CF_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin") #define CF_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end") #else #define CF_ASSUME_NONNULL_BEGIN #define CF_ASSUME_NONNULL_END #endifASSUME_NONNULL_BEGINとかを自前で定義してしまえば良いのでは。 (edited)#if __has_feature(assume_nonnull) #ifndef NS_ASSUME_NONNULL_BEGIN #define NS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin") #endif #endif こういう感じにするのかなと思ってました(将来的に衝突が起きると面倒なので) (edited)NS_はFoundation用のprefixなので、NS_をつけないのが望ましいですね。#pragma に展開されるマクロだと思うので、_Pragmaを定義する必要は無いですね#if __has_feature(assume_nonnull) #define SANO_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin") #define SANO_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end") #endif SANO_ASSUME_NONNULL_BEGIN bool solve(..., const LBMatrixComponent *const _Nonnull *xcomps, ...); SANO_ASSUME_NONNULL_END (edited) __has_feature(assume_nonnull) も絶対YESなのでいらないきがしますが#if __has_feature(assume_nonnull) #define SANO_ASSUME_NONNULL_BEGIN #pragma clang assume_nonnull begin #define SANO_ASSUME_NONNULL_END #pragma clang assume_nonnull end #endif SANO_ASSUME_NONNULL_BEGIN bool solve(..., const LBMatrixComponent *const _Nonnull *xcomps, ...); SANO_ASSUME_NONNULL_END (edited)#define SANO_ASSUME_NONNULL_BEGIN #pragma clang assume_nonnull begin #define SANO_ASSUME_NONNULL_END #pragma clang assume_nonnull end SANO_ASSUME_NONNULL_BEGIN bool solve(..., const LBMatrixComponent *const _Nonnull *xcomps, ...); SANO_ASSUME_NONNULL_END これでいいと思います。#pragma直接書けばいい気もする#ifdef __CLANG__ #define _Pragma(x) #pragma x #else #define _Pragma(x) #endif__CLANG__は今適当に書いた) (edited)#pragma onceはgccでもvcでも使えるな。 #pragma clang assume_nonnull beginは使えないと思いますけど(clangって入ってるし・・・)_Pragmaを使わない#defineは利用時にエラーになるよ。 … .h:17:1: error: expected unqualified-id SB_ASSUME_NONNULL_BEGIN ^ … .h:6:33: note: expanded from macro 'SB_ASSUME_NONNULL_BEGIN' #define SB_ASSUME_NONNULL_BEGIN #pragma clang assume_nonnull begin ^ (edited)bool solve(..., LBVectorComponent **xcomps, size_t *xcomps_cnt);
var xcomps_ptr: UnsafeMutablePointer<LBVectorComponent>? = nil var xcomps_c = 0 if LinBoxWrapper.solve(..., &xcomps_ptr, &xcomps_c) { guard let xcomps_ptr = xcomps_ptr else { fatalError() } let buff = UnsafeBufferPointer(start: xcomps_ptr, count: xcomps_c) defer { xcomps_ptr.deallocate() } return DVector(size: size.cols, components: buff) } else { ... (edited)xcomps_ptrの型は UnsafeMutablePointer<LBVectorComponent>?にしたほうが良いのでは?を使いたい、!型は避けたい。_Nullableは明示したい。clang -Xpreprocessor -fopenmp -lomp myfile.c という風にオプションをつけるとできるそうなのですが,swift build や Xcode からのビルドでこのオプションを設定するのはどうやったらいいか分かりますか?swift build -Xcc -Xpreprocessor -Xcc -fopenmp これは通ってるっぽいです(°_°)-lomp 指定するのはどうしたらいいんでしょう?ld: library not found for -lomp って言われてしまいます… export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/lib してみたんですがダメでした。 (edited)swift run -Xcc -Xpreprocessor -Xcc -fopenmp -Xlinker -L/usr/local/lib -Xlinker -lomp これで実行はできました!…が並列処理は効いてなさそう/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -fobjc-arc -target x86_64-apple-macosx10.10 -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -g -O0 -DSWIFT_PACKAGE=1 -DDEBUG=1 -fblocks -index-store-path /Users/taketo/Projects/spasm/.build/x86_64-apple-macosx/debug/index/store -fmodules -fmodule-name=spasm -I /Users/taketo/Projects/spasm/src/include -fmodules-cache-path=/Users/taketo/Projects/spasm/.build/x86_64-apple-macosx/debug/ModuleCache -Xpreprocessor -fopenmp -MD -MT dependencies -MF /Users/taketo/Projects/spasm/.build/x86_64-apple-macosx/debug/spasm.build/spasm_util.c.d -c /Users/taketo/Projects/spasm/src/spasm_util.c -o /Users/taketo/Projects/spasm/.build/x86_64-apple-macosx/debug/spasm.build/spasm_util.c.o ちゃんと -Xpreprocessor -fopenmp は入ってそう (edited)#pragma omp for for(int n=0; n<1000; ++n) { printf(" %d", n); } printf(".\n"); ↑ この謎の pragma を入れるだけで並列処理になるというwexport OMP_NUM_THREADS=100 これを叩いてから実行したら, 1 65 49 33 13 4 17 77 97 3 9 81 5 73 68 2 69 67 66 45 61 57 41 53 52 37 51 29 36 50 35 21 25 19 20 18 34 6 16 84 46 48 63 59 60 58 56 55 44 54 43 42 40 39 32 38 31 30 28 27 26 24 23 22 15 14 98 99 11 12 8 93 89 10 7 85 80 82 83 76 78 79 75 74 71 72 70 62 64 47 96 95 94 92 90 88 91 87 86 と出ました-Xpreprocessor -fopenmp と指定すればいけました .target( name: "...", cSettings: [ .unsafeFlags(["-Xpreprocessor", "-fopenmp"]) ], linkerSettings: [ .unsafeFlags(["-L/usr/local/lib"]), .linkedLibrary("omp") ] ), (edited)realloc failed: Cannot allocate memory と最後の言葉を残して終了したのですが、こういうメモリエラーをキャッチする方法ってあるんでしょうか? (アプリなら didReceiveMemoryWarning 系のものがあるけど)そもそも RuntimeException 的な仕組みがないのでどう対処したものかとcatch (...) { <= この ... は文字どおり ... です、とかで捕まえられるかも。 それかreallocを入れ替えるとか。/usr/bin/swiftが請け負ってくれているので。
(edited)import SwiftBacktraceを出来るようにはしてありますが。 (edited)coreを使ってスタックトレースを出力、lldb下で実行して出力、が取れる選択肢かな。dockerとかでcore出力を制限されていたりlldbを使えなかったりする場合は実行ファイルが自前で出力するしかないので、SwiftBacktraceを作りました。core出力もlldbも使えません。そのため、ボットが受け取ったコードの実行には使っていませんが、ボット自身のクラッシュを解析する用途としてはSwiftBacktraceを使っています。let semaphore = DispatchSemaphore(value: 0) let mainTask = DispatchWorkItem { while true { print("working...") sleep(1) } } let bgTask = DispatchWorkItem { for i in 0 ..< 3 { print(i) sleep(1) } semaphore.signal() } DispatchQueue.global(qos: .userInitiated).async(execute: mainTask) DispatchQueue.global(qos: .background).async(execute: bgTask) semaphore.wait() mainTask.cancel() bgTask.cancel() print("canceled") sleep(10) // まだ working... が出続けるisCancelled みたいなプロパティがあるのでそれを見て自分でキャンセルします。if isCanceled { // cleanup etc. return } // 処理A if isCanceled { // cleanup etc. return } // 処理B ...let semaphore = DispatchSemaphore(value: 0) var exceedMemory = false DispatchQueue.global(qos: .userInitiated).async { main() semaphore.signal() } DispatchQueue.global(qos: .background).async { while true { let mem = getCurrentRSS() if mem > self.config.maxMemory { exceedMemory = true semaphore.signal() } sleep(10) } } if semaphore.wait(timeout: .now() + timeout) == .timedOut { throw AppError.timeout(sec: config.timeout) } if exceedMemory { throw AppError.exceedMemory(limit: config.maxMemoryGB) }ExpressibleByIntegerLiteral 適合させると予期できない暗黙キャストが起きてカオスになるので、特定の型を fileprivate に ExpressibleByIntegerLiteral に適合できたらいいと思ったのですが、 conditional conformance があると fileprivate extension できないんですね… これは何故なのでしょうpublic protocol InheritingIntegerLiteralExpression: ExpressibleByIntegerLiteral where IntegerLiteralType == Inheritee.IntegerLiteralType { associatedtype Inheritee: ExpressibleByIntegerLiteral init(_ inheritee: Inheritee) } extension InheritingIntegerLiteralExpression { public init(integerLiteral value: IntegerLiteralType) { self.init(Inheritee(integerLiteral: value)) } } (edited)fileprivate extension Polynomial: ExpressibleByIntegerLiteral, InheritingIntegerLiteralExpression where BaseRing: ExpressibleByIntegerLiteral { typealias Inheritee = BaseRing } とするイメージ. (edited)// ファイル A public func something<T: SomeProtocol>(_ value: T) { ... } // ファイルB private extension Foo: SomeProtocol { ... } something(Foo()) 最も単純な例はこれですかね、somethingの実装側からはFooのSomeProtocolに対する実装は見えていないはずなので、実行できなくなりそうです。(できると可視性がおかしなことになってくる)"${PODS_ROOT}/SwiftLint/swiftlint"public class SynchronizedDictionary<Key: Hashable, Value> { private var dictionary: [Key: Value] = [:] private let queue = DispatchQueue(label: "hoge", qos: .default, attributes: .concurrent) public subscript(_ key: Key) -> Value? { get { queue.sync { dictionary[key] } } set { queue.async(flags: .barrier) { self.dictionary[key] = newValue } } } } var a = SynchronizedDictionary<Int, Int>() let n = 100000 DispatchQueue.concurrentPerform(iterations: n) { i in a[i] = i } print(a[n-1]) write のところは sync ではなく async(flags: .barrier) とした方が良いという記事をいくつか見てそうしてます.concurrent にしたらダメじゃないですか? concurrent にした上で write を async(flags: .barrier) にすると write で sync を待たなくて済むのでより良い,というようなことが書かれてたものでsync するように書き直したら一瞬で終わりましたw public class SynchronizedDictionary<Key: Hashable, Value> { private var dictionary: [Key: Value] = [:] private let queue = DispatchQueue(label: "shit", qos: .default) public subscript(_ key: Key) -> Value? { get { queue.sync { dictionary[key] } } set { queue.sync { self.dictionary[key] = newValue } } } } var a = SynchronizedDictionary<Int, Int>() let n = 100000 DispatchQueue.concurrentPerform(iterations: n) { i in a[i] = i } (edited)a[i] = i print(a[i]) のようにgetが呼ばれるコードを後に足すと終了すると思います。start end: 0.481503963470459 Program ended with exit code: 0print(a[n-1]) のところを消しても同じぐらいかかりますね.public class SynchronizedDictionary<Key: Hashable, Value> { private var dictionary: [Key: Value] = [:] private let queue = DispatchQueue(label: "shit", qos: .default, attributes: .concurrent) public subscript(_ key: Key) -> Value? { get { queue.sync { dictionary[key] } } set { queue.async(flags: .barrier) { [weak self] in self?.dictionary[key] = newValue } } } } var a = SynchronizedDictionary<Int, Int>() let n = 100000 DispatchQueue.concurrentPerform(iterations: n) { i in a[i] = i } print("HELLO?") print(a[n-1]) 上のコードは HELLO? はすぐ出力されて,次の a[n - 1] の読み取りで待たされるはずだと思ったんですが,HELLO? も待たされるんですよね. (edited)syncが呼び出しスレッドで実行されるのはqueueに実行待ちのアイテムが無い時で、asyncでアイテムが登録されてるとasyncで登録されたアイテムが実行されるスレッドで実行されるのをsyncが待つ様な実装になってたと記憶。
(edited).concurrentだと違うのかな? (edited)async用のスレッドで実行する場合があったかと。
override func viewDidLoad() { super.viewDidLoad() self.tableView.isScrollEnabled = false } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { if scrollView == self.scrollView { tableView.isScrollEnabled = (self.scrollView.contentOffset.y >= 200) } if scrollView == self.tableView { self.tableView.isScrollEnabled = (tableView.contentOffset.y > 0) } }scrollIndicatorInsets をセットした↑scrollViewDidEndDecelerating が拾えなくなってしまうと思います実装例: class ExampleViewController: ButtonBarPagerTabStripViewController, UITableViewDelegate { (中略) override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { let child_1 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: "FRIENDS")) child_1.tableView.contentInset.top = kHeaderHeight child_1.tableView.delegate = self let child_2 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: "FEATURED")) child_2.blackTheme = true return [child_1, child_2] } (中略) override func scrollViewDidScroll(_ scrollView: UIScrollView) { let acutualY = scrollView.contentOffset.y + kHeaderHeight buttonBarView.frame.origin.y = 200 - acutualY } ※例なので 200 とか数値をそのまま埋め込んじゃってます (edited)initial commitと書いてあるコミットは9月だなw (edited)withUnsafeMutableBufferPointer を使った書き換え処理で、内部でスライスを作って書き換えるのはダメだと知ったのでシェアしますvar a = ContiguousArray(repeating: 1, count: 10) a.withUnsafeMutableBufferPointer { buff in for i in 3 ..< 6 { buff[i] *= 2 // 問題なし } } a.withUnsafeMutableBufferPointer { buff in var sub = buff[3 ..< 6] for i in 0 ..< sub.count { sub[i] *= 2 // EXC_BAD_ACCESS } } (edited)a.withUnsafeMutableBufferPointer { buff in var sub = buff[3 ..< 6] for i in sub.indices { sub[i] *= 2 } }a[3..<6].withUnsafeMutableBufferPointer { buff in for i in 0..<buff.count { print(i) buff[i] *= 2 } } こういうのもできます。buff[.start + i] って書けるようにする案が審査落ちしました。let package = Package( name: ... dependencies: [ .package(url: "https://github.com/apple/swift-tools-support-core", .branch("master")) ], targets: [ .target( name: "...", dependencies: ["SwiftToolsSupport"] ), で、 ArgumentParser を使いたいとこで import TSCUtility でいけましたwithUnsafeMutableBufferPointer を使ったやり方では、最初から let threads = 8 とか決めうちしちゃって、大っきな ContiguousArray 作ってスライスしたものを各スレッド(もどき)で使い回す、というようなことをしたのですが。enum hogeError: Error { case hoge var localizedDescription: String { "hoge" } } func sayError(_ e: Error) { print(e.localizedDescription) } sayError(hogeError.hoge) sayErrorメソッド内でhogeErrorにキャストすれば実現できますが、 発生し得るエラーの種類が多いため、すべて列挙するのはあまり現実的でありません。 うまい解決方法はないでしょうか。import Foundation enum hogeError: LocalizedError { case hoge var errorDescription: String? { "hoge" } } func sayError(_ e: Error) { print(e.localizedDescription) } sayError(hogeError.hoge)Errorの代わりにFoundation.LocalizedErrorに準拠して、errorDescriptionの実装を与えると、 .localizedDescriptionが変更できますよ。import Foundationやimport UIKitなどを書かなければfunc sayError(_ e: Error) { print(e.localizedDescription) } ↑これがそもそもコンパイルエラーになります.localizedDescriptionはFoundation.NSErrorのメソッドで、Swift.Errorには無いメソッドだからです。import Foundationすると、ErrorからNSErrorへの暗黙の変換が起こるようになって、呼び出せるようになる。@Binding か @ObservedObject で用意して、TextのBindingにその変数を指定します。 これで変数として持っている状態がTextに自動的に反映されるようになります。 あとは非同期処理が完了するタイミングで適宜その変数を更新します。ld: warning: Could not find or use auto-linked library 'swiftDarwin' ld: warning: Could not find or use auto-linked library 'swiftCoreFoundation' ld: warning: Could not find or use auto-linked library 'swiftObjectiveC' ld: warning: Could not find or use auto-linked library 'swiftDispatch' ld: warning: Could not find or use auto-linked library 'swiftCoreImage' ld: warning: Could not find or use auto-linked library 'swiftQuartzCore' ld: warning: Could not find or use auto-linked library 'swiftCoreGraphics' ld: warning: Could not find or use auto-linked library 'swiftCore' ld: warning: Could not find or use auto-linked library 'swiftMetal' ld: warning: Could not find or use auto-linked library 'swiftFoundation' ちなみに, 同様のMac, Xcodeの環境で新規プロジェクトを作成してSwiftGenをpod installした場合は上記のwarningは出ません. どこから手をつけたら良いか, どこの部分の理解が足りてなくて解決できないのかわからずという状態です. (edited)@GestureStateについて追記しました。BackgroundTask、 ●Background Processing Tasks - 数分かかる処理 - 延期可能なメンテ処理やCoreMLによるトレーニングを想定 ●Background App Refresh Tasks - 30秒のランタイム - アプリを最新の状態に保つための用途を想定 - performFetchのAPI が代わりにdeprecatedに みたいな感じの仕様らしいのですが、background processの数分ってのは実際どれぐらい処理が続く感じなんでしょうか。 また BackgroundTask では実現できないなら他の案をご教授いただきたいです。 既存アプリのGoogleフォトとかはバックグラウンドで常に写真のアップロードを監視してたりするのでできないことはないと思うのですが、どうやってバックグラウンドで常時処理をするのかが気になりました。よかったらどなたか強い人回答お願いします。 https://github.com/nshhhin/Background-Accel-Swift .systemLibrary( name: "Cm4ri", path: "Libraries/Cm4ri" ), を追加し, dependencies に "Cm4ri" を追加した上で, Libraries/Cm4ri/module.modulemap
module Cm4ri [system] { header "/usr/local/include/m4ri/m4ri.h" link "m4ri" export * } を追加しました.コマンドラインから "swift run" すると通るのですが,generate-xcodeproj して実行しようとすると,include される m4ri.h の中で "'m4ri/mzp.h' file not found" というエラーが出てビルドできませんautoreconf --install ./configure --enable-openmp make make check make install しました. (edited)make install のインストール先をプロジェクト内にしてしまって、brew install libomp libtool もしました../configure --prefix=$PWD/dist make install ^ のようにすると$PWD/distにライブラリがインストールされます。./configure --prefix=$PWD/dist --enable-openmp ビルドが終わるとdistは以下のようになります。 dist ├── include └── libdist ├── include │ └── m4ri │ ├── brilliantrussian.h │ ├── debug_dump.h │ ├── djb.h │ ├── echelonform.h │ ├── graycode.h │ ├── io.h │ ├── m4ri.h │ ├── m4ri_config.h │ ├── misc.h │ ├── mmc.h │ ├── mp.h │ ├── mzd.h │ ├── mzp.h │ ├── parity.h │ ├── ple.h │ ├── ple_russian.h │ ├── ple_russian_template.h │ ├── solve.h │ ├── strassen.h │ ├── triangular.h │ ├── triangular_russian.h │ ├── xor.h │ └── xor_template.h └── lib ├── libm4ri-0.0.20140914.dylib ├── libm4ri.a ├── libm4ri.dylib -> libm4ri-0.0.20140914.dylib ├── libm4ri.la └── pkgconfig └── m4ri.pclibm4ri.aだけなんですけど、 ^ のdistディレクトリを自分のプロジェクトの中に入れてしまって (configureでそうなるようにして)SwiftPMからはそれを参照するようにすると、SystemLibraryとか気にせずに単にプロジェクト内にあるStatic Libを参照するだけ、となるはず。(というのを今試しています). ├── Libraries │ └── Cm4ri │ ├── include │ │ └── m4ri │ ├── lib │ │ └── libm4ri.a │ └── module.modulemap ├── Package.swift ├── README.md ├── Sources │ └── shit │ └── main.swift └── Tests ├── LinuxMain.swift └── shitTests ├── XCTestManifests.swift └── shitTests.swift リポジトリの構成をこのようにするとしてmodulemapは例えばこうなる module Cm4ri { header "include/m4ri.h" link "m4ri" } (modulemapはいろんな解があるんでいったん簡単なやつで試す) (edited)# 構成 ├── UITableView │ └── VideoCell(UITableViewCell継承のCustomClass) │ ├── currentTime(動画再生時間を保持するメンバ) │ ├── VideoView(UIView継承のCustomClass) │ │ └── AVPlayer │ │ └── AVPlayerItem │ │ └── AVPlayerLayer
# 作りたいもの Twitterアプリのタイムラインで表示される動画のUI # 機能 UITableView上で動画を再生・一時停止 1. スクロール時、動画のcellが画面内に収まっている場合は動画を再生 2. スクロール時、動画のcellが画面外に出ている場合は動画を一時停止 # やっている事 スクロール時に再生・一時停止の処理 1. ScrollViewのdelegate `scrollViewDidScroll` で最上部・最下部のCGPointを取得 2. 1で取得したCGPointからindexPathを取得 3. 2で取得したindexPathからcellを取得 4. 3で取得したcellのframeと画面上部、画面下部のcontentOffset.yを計算して、収まっている or 収まっていないを判定 5. 判定結果により、動画を再生 or 一時停止 # うまくいかない所 一度再生した動画セルが画面外に行き、再度画面内に来た時にスムーズに続きから再生されない a. AVPlayerItemに動画URLを設定 b. AVPlayerのreplaceCurrentItemで設定 c. AVPlayerのseekで保持していた再生時間を指定(そのまま指定すると反応しない?のでDispatchで遅延実行しています)dequeueReusableCell を使用せずに、cellを生成すれば良いのでしょうか?protocol Cat があって KawaiiNeko: Cat と CuteNaNeko: Cat は作ってあるけど勝手に KawaikunaiNeko: Cat は作られたくないんです。protocl じゃなくてもなんらかの表現でこの制約が与えられた良いのですが...enumは近そうなんですがちょっとキツい感じが。enum Cat { case kawaiiNeko(KawaiiNeko) case cuteNaNeko(CuteNaNeko) } protocol CatProtocol { } struct KawaiiNeko: CatProtocol {}enum Cat に対して CatProtocol のメソッドを全部実装しちゃえば、extension Cat { func asCatProtocol() -> CAtProtocol }open classではなくpublic classでサブクラス化を抑制できそうですねtryがあります。 このような場合インターフェースとしてはどのようなものが好ましいと思いますか? 1. throwsにする ユーザー側で毎回ハンドリングが必要なのでロガーとしてはいまいちな気がします。 2. 中でcatchして別途設定したハンドラに渡す 今はこれになっています。ハンドラはエラーだけを取っていますが、コールスタック等の情報も入れたほうがいいのかと考えています。 (edited)do-catch { errorHandler() } が何度も出現してるから、実装は高階関数にしたほうが良さそうと思った あと合成する場合は一番上だけでcatchfunc addText(...) { wrap { try w.addText(...) } }try wrap(w.addText(:))(...) こういうのもできるかもwstruct CategoryHome: View { var categories: [String: [Landmark]] { Dictionary( grouping: landmarkData, by: { $0.category.rawValue } ) } var featured: [Landmark] { landmarkData.filter { $0.isFeatured } } ... }struct CategoryHome: View { var categories: [String: [Landmark]] { get { return Dictionary( grouping: landmarkData, by: { $0.category.rawValue } ) } } var featured: [Landmark] { get { return landmarkData.filter { $0.isFeatured } } } ... }categories, featured はComputed Propertyと呼ばれるもので、Property(プロパティ、メンバ変数、インスタンス変数)の一種ですが、実際の値を変数に保持しているわけではなく、アクセスがあったときに書かれている処理を実行して動的に値を返すことができる、というものです。let home = CategoryHome() let categories = home.categories このように通常のプロパティと同じようにアクセスしますが、 Computed Propertyはアクセスされたときに書かれている処理(ここでは get { ... } の中カッコの部分)を実行して値を返します。get と set の両方を定義できて、この例ではget の場合だけが定義されています。 (edited)getしかない場合はget { ... } と書くのを省略できて、中カッコだけにできます。 (edited)struct CategoryHome: View { var categories: [String: [Landmark]] { return Dictionary( grouping: landmarkData, by: { $0.category.rawValue } ) } ... }struct CategoryHome: View { var categories: [String: [Landmark]] { Dictionary( grouping: landmarkData, by: { $0.category.rawValue } ) } ... }func boardView(_ boardView: BoardView, didSelectCellAtX x: Int, y: Int) ですね。ViewController 中の当該部分は↓です。 https://github.com/refactoring-challenge/reversi-ios/blob/master/Reversi/ViewController.swift#L388-L402AVAssetWriterInput とかですかねー。AVVideoCompositionCoreAnimationTool かあ...AVAssetExportSession はバグ多いんですが、#if os(iOS) to conditionally import this framework when building for iOS. No such module 'GitHub' Xcodeを使ってライブラリをリンクさせたことがありませんので、何か基本的な事を間違えているのだと思いますが、どなたか教えていただけると幸いです。
1#import GitHub と書いた部分にエラーが出ましたLink Binary With Libraries セクションでGitHub.frameworkが選べるようになると思います。GitHub と Github で H の大文字小文字がときどきブレてるのが気になります。 家庭くんと岸川さんの書いてるように、 フレームワークファイルを手作りしないで済む方法(プロジェクト依存またはCocoaPods)のほうが無難です。GitHub.xcworkspace ひらけば何もせずにGitHub scheme一回ビルドしてから palygorundのやつ全部動くと思いますよGeneric iOS Device のところをシミューレータにするとシミュレーター向けのビルドになりますgit clone --depth=1 https://github.com/peaks-cc/iOS_architecture_samplecode.git して試したので。macOS 11.15.4, Xcode 11.4.1。 (edited)GitHub > Generic iOS DeviceiPhone 11 Pro Max とかに変えてclass SomeClass { init(cancelHandler: (() -> Void)?, submitHandler: (() -> Void)?) { } }class Parent { var child: Child init(child: Child) } extension Parent: Autohority {}
protocol Authority: AnyObject {} class Child { weak var autority: Authority? init(authority: Authoority) } こうかですね↑ (edited)Parent 作るのに Child 必要、でも Child を作るのに Authority として Parent が必要class ViewController: SomeViewDelegate { private lazy var presenter = Presenter(delegate: self) } class Presenter { private weak var delegate: SomeViewDelegate! init(delegate: SomeViewDelegate) { self.delegate = delegate } } ちなこんなの作ってました ViewController の方のlazy varのprivateを外すといいのかな (edited)struct ResponseA { let isHoge:Bool } struct ResponseB { let str:String } func requestA() -> Observable<ResponseA> { //通信処理 } func requestB() -> Observable<ResponseB> { //通信処理 }class ViewController: UIViewController { let disposeBag: DisposeBag = DisposeBag() @IBAction func onButton() { requestA().flatMap({ (responseA) -> Observable<Void> in requestB().map({ (responseB) -> Void in if responseA.isHoge, responseB.str == "xxx" { accept(responseA, responseB) } return () }) }).disposed(by: disposeBag) } }func accept(responseA: ResponseA, responseB: ResponseB) { ... } だったとしたら、という擬似コードでした。struct DirectProduct<Left: Sequence, Right: Sequence> { let left: Left let right: Right } このようなstructをSequenceにしたいと思うのですが、この時Left, Rightの型によってIterator#next()の実装を変えたいと思うのですが、これは可能でしょうか? 考えているのはLeft,RightがCollectionの時とLeft,RightがCollectionでIndex == Intの時に実装を切り替えることです。 いろいろ試しているのですがうまくいきません。 方法があれば教えてください。struct DirectProduct<Iterator, Element, Left: Sequence, Right: Sequence> where Left.Element == Element, Right.Element == Element, Left.Iterator == Iterator, Right.Iterator == Iterator { let left: Left let right: Right } extension DirectProduct: Sequence where Left: Collection, Right: Collection { func makeIterator() -> Iterator { if left.startIndex is Int, right.startIndex is Int { // return iterator } else { // return another iterator } } }extension ではできなさそうですね。 @swift-5.2.5
struct DirectProduct<Left: Sequence, Right: Sequence> { let left: Left let right: Right init(_ left: Left, _ right: Right) { self.left = left self.right = right } } extension DirectProduct: Sequence { func makeIterator() -> Iterator { print("makeIterator A") return Iterator(left, right) } class Iterator: IteratorProtocol { fileprivate let left: Left fileprivate let right: Right init(_ left: Left, _ right: Right) { self.left = left self.right = right } func next() -> (Left.Element, Right.Element)? { nil } } } extension DirectProduct where Left: Collection, Right: Collection, Left.Index == Int, Right.Index == Int { func makeIterator() -> Iterator { print("makeIterator B") return Iterator2(left, right) } class Iterator2: Iterator { override init(_ left: Left, _ right: Right) { super.init(left, right) } override func next() -> (Left.Element, Right.Element)? { nil } } } do { let p = DirectProduct([2, 3, 5], ["X", "Y", "Z"] as Set<String>) for (_, _) in p {} _ = p.makeIterator() } print() do { let p = DirectProduct([2, 3, 5], ["X", "Y", "Z"]) for (_, _) in p {} _ = p.makeIterator() }makeIterator A makeIterator A makeIterator A makeIterator Bstruct DirectProduct<Left: Collection, Right: Collection> { let left: Left let right: Right let iteratorType: DirectProductIterator<Left, Right>.Type init(_ left: Left, _ right: Right) { self.left = left self.right = right iteratorType = DirectProductIterator.self } } extension DirectProduct where Left.Index == Int, Right.Index == Int { init(_ left: Left, _ right: Right) { self.left = left self.right = right iteratorType = DirectProductIterator2.self } } extension DirectProduct: Sequence { func makeIterator() -> DirectProductIterator<Left, Right> { iteratorType.init(left, right) } } class DirectProductIterator<Left: Collection, Right: Collection>: IteratorProtocol { fileprivate let left: Left fileprivate let right: Right required init(_ left: Left, _ right: Right) { self.left = left self.right = right } func next() -> (Left.Element, Right.Element)? { print("DirectProductIterator") return nil } } class DirectProductIterator2<Left: Collection, Right: Collection>: DirectProductIterator<Left, Right> where Left.Index == Int, Right.Index == Int { required init(_ left: Left, _ right: Right) { super.init(left, right) } override func next() -> (Left.Element, Right.Element)? { print("DirectProductIterator2") return nil } } do { let p = DirectProduct([2, 3, 5], ["X", "Y", "Z"] as Set<String>) for (_, _) in p {} } print() do { let p = DirectProduct([2, 3, 5], ["X", "Y", "Z"]) for (_, _) in p {} }DirectProductIterator DirectProductIterator2for では無理だったのでイニシャライザで型情報を埋め込みました。makeIterator を無駄に呼んでるのが気持ち悪いですが・・・。Left や Right に参照型のコレクション渡されると壊れるけど。Sequence と Collection でもいける気がしますね。Sequence で Iterator1 、 Collection where Element == Int で Iterator2 が返されるようにしました。 https://gist.github.com/koher/550ea1e9625e66758bac85eb5063196fimport SwiftUI struct ContentView: View { var body: some View { VStack { Text("Hello SwiftUI Playground!") .font(.largeTitle) .foregroundColor(.blue) .padding() Button(action: {}) { HStack { Image(systemName: "suit.heart.fill") .foregroundColor(.red) Text("Let's Get Started!") .font(.headline) .foregroundColor(.white) } .padding(12) .background(Color.orange) .cornerRadius(8) } } } }import SwiftUI struct ContentView: View { var body: some View { let button = Button(action: {}) { HStack { Image(systemName: "suit.heart.fill") .foregroundColor(.red) Text("Let's Get Started!") .font(.headline) .foregroundColor(.white) } .padding(12) .background(Color.orange) .cornerRadius(8) } let text = Text("Hello SwiftUI Playground!") .font(.largeTitle) .foregroundColor(.blue) .padding() return VStack { button text } } } このように入れ子になっているクロージャを取り出していったん変数に格納してそれを同じように最後に組み合わせるようにします。 こうすると、もともと入れ子になっていた1つ1つのクロージャは簡単なクロージャでいったん型の推測が可能になるのでだいたい問題が解決します。無理な場合はもっと細かく分解します。scrollView.setContentOffset にスクロールさせたい 座標をCGPointで渡せばいいことまではわかったのですが xib内のUIコンポーネントの座標を渡してみたところ 常にその座標が(x:0.0,y:0.0)になってしまい 自分が想定しているところまでスクロール出来ず困っています 具体的に、スクリーンショットの Second Input Viewの黒字のタイトルと書かれている UILabelの上までスクロールさせたいのですが この座標を取得するにはどうしたら宜しいでしょうか? 黄色塗りされている部分がxibとなっております。 ご教示頂けると幸いです。宜しくお願いします。 (edited)let frame = secondInputView.titleLabel.convert(secondInputView.titleLabel.bounds, to: scrollView) print(frame) ^ このコードを追加して値を調べてみてください。titleLabel.frame はSuper Viewからの相対座標なので、secondInputViewから見た座標になります。 Scroll Viewでどの位置か、というのはScrollViewから見た座標に変換する必要があります。(20.0, 174.66666666666669, 63.66666666666666, 20.333333333333343) になり frame.origin.y を渡してあげたら、想定した位置までスクロールしました! 調べた中でconvertは出てきたんですけど、ややこしくていまいち理解が出来ずに 避けてしまったので、もう一度ドキュメントをよく見てみます。 非常に助かりましたありがとうございました! (edited)UIGestureRecognizerのdelegate設定したいいんじゃないでしょうか? (edited)UICollectionViewは(UITableViewとかも)コンテンツへのタップデフォルトでは遅らせるようになっていて(だからスクロール中にボタンとかに反応せずにスクロールがスムーズにできる) (edited)modalTransitionStyle にcrossDissolve を指定してpresent で遷移したときの フワッと遷移する際の、durationの値を変更することは可能でしょうか? また不可能な場合、代替策としてどのような実装がありますでしょうか・・・? ご教示頂けると幸いです。viewController.modalPresentationStyle = .custom viewController.transitioningDelegate = self としてカスタムトランジションを実装するのが一般的な方法です。 クロスディゾルブくらいなら遷移元と先でalphaを1=>0/0=>1するだけなので実装も難しくないです。CATransaction.setAnimationDuration()あたりでまずは試してみると雰囲気わかるかもimport UIKit import MediaPlayer var printnumber: Int = 1 class MediaViewController: UITableViewController, MPMediaPickerControllerDelegate { fileprivate let numberOfRingtones = 3 var mediaItem: MPMediaItem? var mediaLabel: String! var mediaID: String! override func viewDidLoad() { super.viewDidLoad() } override func viewWillDisappear(_ animated: Bool) { //performSegue(withIdentifier: Id.soundUnwindIdentifier, sender: self) } ... func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection:MPMediaItemCollection) -> Void { if !mediaItemCollection.items.isEmpty { let aMediaItem = mediaItemCollection.items[0] self.mediaItem = aMediaItem mediaID = (self.mediaItem?.value(forProperty: MPMediaItemPropertyPersistentID)) as? String self.dismiss(animated: true, completion: nil) } } func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) { self.dismiss(animated: true, completion: nil) } } AlarmAddEditViewController.swift @IBAction func unwindFromMediaView(_ segue: UIStoryboardSegue) { let src = segue.source as! MediaViewController segueInfo.mediaLabel = src.mediaLabel segueInfo.mediaID = src.mediaID } (edited)UITabelViewDataSouce で調べてみてくださいMediaViewControllerがUITableViewControllerとして作られているので、それ自身がUITableViewDataSourceになっています。MPMedoaPickerControllerで選択されたMPMediaItemを誰かがUITabelViewCellにしないと結果として表示されません...MPMedoaPickerControllerで選択されたMPMediaItemを誰かがUITabelViewCellにしないと結果として表示されません... tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCellで、なにかしらする必要があります。mediaIDは多分なんか識別用の内部的なヤツで表示しても意味ないかも。 (edited)cell.titleLabel.text = mediaItem.title じゃダメですかね//Data.swift import SwiftUI import Combine class HistoryEntity: ObservableObject{ @Published var UserName:[String] = [] } //AddView.swift @EnvironmentObject var histroy: HistoryEntity var body: some View { NavigationView { Form { Picker(selection: $Selection, label: Text("name")) { ForEach(0..<histroy.UserName.count){ Text(self.histroy.UserName[$0]) } } } } (edited)let contentView = ContentView().environmentObject(HistoryEntity())//ContentView.seift struct ContentView: View { @EnvironmentObject var histroy: HistoryEntity var body: some View { NavigationView { List { ForEach(histroy.UserName, id: \.self){ user in NavigationLink(destination: HistoryView(name: user)){ Text(user) } } } } //HistroyView.swift struct HistoryView: View { @State private var isShowingSheet: Bool = false var name: String = "" var body: some View { List { VStack { Text("") } NavigationLink(destination: DetailsView()) { HistroyRowView() } .navigationBarTitle(name) .navigationBarItems(trailing:HStack { Button(action: { self.isShowingSheet = true }) { Text("") } .sheet(isPresented: $isShowingSheet){ AddView(isShowingSheet: self.$isShowingSheet) } } } }Thread 1: Fatal error: No ObservableObject of type HistoryEntity found. A View.environmentObject(_:) for HistoryEntity may be missing as an ancestor of this view.''' これはどこで起こってるんですか?ForEach(0..<histroy.UserName.count){ ←この行に表示されています (edited)Button(action: { self.isShowingSheet = true })//AddView.swift @EnvironmentObject var histroy: HistoryEntity これでは渡せていないということでしょうかlet contentView = ContentView().environmentObject(HistoryEntity()) ContentViewに渡してるようなこういうenvironmentObjectのModifierを使ってる部分です。EnvironmentObject って自動的に共有されるんじゃないですっけ?@ObservedObject 使ってるのであまり自信ないですが)AddView(isShowingSheet: self.$isShowingSheet).environmentObject(self.histroy) なのでこう書くとまあ解決します。 サブビューなら伝播してくれるんですけど、sheetは別のビュー階層扱いなんでしょうね。NavigationLink は大丈夫なのに .sheet はダメなんですね・・・。NavigationLink は引数に渡すからつながってるけど、 .sheet はクロージャだから途切れてるのか。HistoryEntity を AddView から先で使わないなら、 AddView は @EnvironmentObject じゃなくて @ObservedObject で持ってもいいかもしれませんね。struct AddView: View { @ObservedObject var histroy: HistoryEntity ... }
Text("追加") } .sheet(isPresented: $isShowingSheet){ AddView(histroy: self.histroy, isShowingSheet: self.$isShowingSheet) }})histroy は history の typo だと思いますが元のままにしてます・・・)@EnvironmentObject は複数の View の間で広く(暗黙的に)共有するためのもので、 @ObservedObject は必要なときに明示的に渡すものです。この場合、 .sheet で @EnvironmentObject が途切れてしまうようなので、 AddView にだけ渡すのであれば @ObservedObject で十分かなと思いました。 AddView から先でも広く共有したい種類のものであれば @EnvironmentObject が良いと思います。@EnvironmentObject だからといって大きな不利益があるわけではないですが、必要なものは必要最小限のスコープで狭く使っておいた方が管理が簡単だと思うので、 @EnvironmentObject である必要がないのであれば @ObservedObject でもいいのかなと思いました。 (edited)print("selected")が出力されないので悩んでいます。 UIkit側のイベントをSwiftUIが受け取れていないことが原因だと思いますが、解決策はありませんでしょうか。 import SwiftUI import FSCalendar import UIKit class ViewController: UIViewController, FSCalendarDelegate, FSCalendarDataSource{ @IBOutlet var calender: FSCalendar! override func viewDidLoad() { super.viewDidLoad() calender.delegate = self } func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { print("selected") } } struct UICalender: UIViewRepresentable { func makeUIView(context: Context) -> FSCalendar { return FSCalendar() } func updateUIView(_ uiView: FSCalendar, context: Context) { } } struct CalenderView: View { var body: some View{ UICalender() } } struct CalenderView_Previews: PreviewProvider { static var previews: some View { CalenderView() } }@IBOutlet var calender: FSCalendar! とは別のインスタンスであり、したがってdelegateはどこにも登録されていません。 ViewControllerのprintを呼び出したいのであれば、UIViewControllerRepresentableを利用し、そこでUIStoryboardからViewControllerのインスタンスを生成して返す必要があります。import Instantiate struct ViewControllerWrapper : UIViewControllerRepresentable { typealias UIViewControllerType = ViewController func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerWrapper>) -> ViewControllerWrapper.UIViewControllerType { return UIViewControllerType.instantiate() } func updateUIViewController(_ uiViewController: ViewControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<ViewControllerWrapper>) { } } こちらを追加してみたのですが、Instantiate()に対して「Type 'ViewControllerWrapper.UIViewControllerType' (aka 'ViewController') has no member 'instantiate'」というエラーが出てしまいます、、extension ViewController: StoryboardInstantiatable {} の宣言が存在していないのではextension ViewController: StoryboardInstantiatable{ typealias StoryboardInstantiatableViewController = ViewController static var storyboardName: String { return "Main" } } を追加してみましたが、printは動作しないままでした、、 var body: some View{ UICalender() } を var body: some View{ ViewControllerWrapper() } に変更したら理想の動作になりました! 何度も質問に答えていただきありがとうございました!貴重なお時間を割いていただき、ありがとうございました!Date で検索して取得したいという話ではないですか? (edited)Date は年月日じゃなくて秒とかのもっと細かい情報を持っているので、同一年月日だとしてもデータが一致するとは限らないので注意が必要です。"2020-08-11" とか)を保存するようにして、文字列で検索しますね。 Core Data でやったことはないので、 Core Data で何か良い方法があるかもしれませんが。return[]の行に「Will never be executed」と出ます。 ・fetchEmployees()を使いたいのですが、 DataController.fetchRecordEntity() の()内に何を入力すればいいかわかりません。エディタには(self:DataController)と表示されます。DataController.fetchRecordEntity() はクラスメソッドを呼び出してしまっているので、 DataContorller のインスタンスを生成してインスタンスメソッドを呼び出す必要があるように見えます。(ただし、 Core Data を満足に使ったことがないのでわかってないですが)return [] まで到達できないコードになっているのではないでしょうか?
DataController.fetchRecordEntity() 一つにしても、インスタンスとクラスの関係がわかっていないと理解が難しいですし、func fetchEmployees() -> [EmployeeMO] { let context = persistentContainer.viewContext let employeesFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Employee") do { let fetchedEmployees = try context.fetch(employeesFetch) as! [EmployeeMO] return fetchedEmployees } catch { fatalError("Failed to fetch employees: \(error)") } return [] }return fetchedEmployees か fatalError("Failed to fetch employees: \(error)") で必ず終了するからです。try context.fetch(employeesFetch) に try がマークされているので、ここでエラーが発生したときに catch 節に入るということがわかります。なので、エラーが発生しなければ return fetchedEmployees 、発生したら fatalError("Failed to fetch employees: \(error)") で fetchEmployees() は終了します。return [] が実行されることはなく、コンパイラが will never be executed (決して実行されることがない)と警告しているのです。try や as! の意味は? fatalError で何が起こる? fatalError の戻り値の型の Never とは?どういうケースで will never be executed の警告が出るか)NSPredicate (これ自体は Core Data とは独立に Foundation に存在します)@Environment などが用いられているのですが、おそらく何かのコードをみようみまねで書いたのだと思いますが、それらについてどう書くべきかを理解するには SwiftUI について学ぶ必要がありますし、そのためには Property Wrapper や Combine の理解も必要になります。class Interactor { var name: String init(name: String) { self.name = name } } class FirstPresenter { var interactor: Interactor init(interactor: Interactor) { self.interactor = interactor } } class SecondPresenter { var interactor: Interactor init(interactor: Interactor) { self.interactor = interactor } } // メイン処理 let interactor = Interactor(name:"aaa") let firstPresenter = FirstPresenter(interactor: interactor) let secondPresenter = SecondPresenter(interactor: interactor) print(firstPresenter.interactor.name) // aaa print(secondPresenter.interactor.name) // aaa firstPresenter.interactor.name = "bbb" print(firstPresenter.interactor.name) // bbb print(secondPresenter.interactor.name) // bbb secondPresenterからすると、自身の知らないところでinteractorが更新されてて困るケースがあるというのは理解できます。 一方で、複数のPresenterで同じInteractorを共有したい場合は有用かなとも思ったのですが、皆さんはどうやって実現されているのでしょうか?HogeFugaDataStore とかの名前にします)にするのでも良いのかなと思いました! 投稿が初めてでして、不明点等あったら突っ込んでいただけると助かります! (edited)FirstPresenter からの入力による変更によって SecondPresenter が何かを View に出力しなければならないのであれば、それが Interactor から SecondPresenter に通知される必要があるのではないでしょうか。
InteractorOutput のメソッドを呼ぶような形かもしれませんし、 name が直接出力に使われるのであれば(たとえば Combine を使うなら) name を Publisher にしておいてそれを購読するとか、もしくは Interactor 自体を ObservableObject にするとか、色々な方法があると思います。Presenter は Interactor を保持するようですし(上記ページによるとプロトコルを介するのが普通なのかもしれませんが)、 Interactor は一つの View に紐づくわけではないですよね?そうすると複数の Presenter が Interactor を共有してもおかしくないと思います。Presenter は Interactor からの通知を受けて、それを View に適した形に変換して通知するものだと思うので、 Presenter からの入力(上図の asks for updates )を受けて Interactor が更新されたときに、必要に応じて Interactor は Presenter に更新を通知する必要があると思います。MainViewController はどうなってるでしょうか?
1*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x105a060c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key videoPreview.' terminating with uncaught exception of type NSException1x 2x 3x のところに画像をD&Dしてあげれば、本来でないです。 D&Dしてセットしたあとに画像のファイル名変更等するとその警告はでるので、ファイル名変更するなら変更後にD&Dするといいかと。Unsigned の画像は選択してdeleteでいいと思います。 それでこの警告自体はきえるかと。
1swift カメラ qrコード で検索すると記事がいくつも出てくるので、参考にしてやってみてください。 metadataOutput.metadataObjectTypes = [.qr].xcworkspaceを開けば、今まで開発を進めていたxcodeprojにライブラリを取り込んだ状態で構成して開かれます。なので、xcworkspaceを開いてそのまま開発継続して問題ないです (edited)becomeFirstResponder() をプログラムから呼んでもキーボードが立ち上がらない」という問題に直面しています。。タップイベントでキーボードが起動できた後なら問題ないのですが、初回だけ起動されず。 同じような経験をした方、または解決方法をご存知の方はいらっしゃいますでしょうか? こちらのstackoverflow(https://stackoverflow.com/questions/59582631/becomefirstresponder-is-not-showing-keyboard-until-touch-of-any-uitextfield-at-l )と同じ現象になんですが、解決策で提示されているIntercomを使っていないので解決できず困っている状況です。。import Foundation // 年月日 struct YMD: Codable { var year: Int var month: Int var day: Int } // 日付と体重・体脂肪率の記録 struct Record: Codable { var ymd: YMD var weight: Double // kg var fatRate: Double // 0.0 - 1.0 } // 日々の体重の記録は↓のような [Record] として扱える let records: [Record] = [ Record(ymd: YMD(year: 2020, month: 10, day: 14), weight: 55.2, fatRate: 0.121), Record(ymd: YMD(year: 2020, month: 10, day: 15), weight: 55.4, fatRate: 0.124), Record(ymd: YMD(year: 2020, month: 10, day: 16), weight: 55.5, fatRate: 0.123), ] // JSON ファイルから [Record] を読み込んで追加し保存する一連の処理 do { // ファイルから JSON 形式のバイト列を読み込み let data: Data = try Data(contentsOf: URL(fileURLWithPath: "path/to/records.json")) // JSON から日々の体重の記録を復元 var records: [Record] = try JSONDecoder().decode([Record].self, from: data) // 今日の記録を追加 let record: Record = Record(ymd: YMD(year: 2020, month: 10, day: 17), weight: 55.6, fatRate: 0.125) records.append(record) // JSON 形式のバイト列に変換 let newData: Data = try JSONEncoder().encode(records) // ファイルに保存 try newData.write(to: URL(fileURLWithPath: "path/to/records.json"), options: .atomic) } catch { // エラーハンドリング } (edited)Record や YMD のように Codable に適合した型であれば JSONEncoder や JSONDecoder を使って JSON 形式のバイト列に変換・復元することが簡単にできます。Codable が何をするのかということを学ぶと理解が深まると思います。(このコードを使っていただくこと自体は問題ありません。) (edited) func sliderWidth(SliderWidth: CGFloat, SidewaysAlert: Bool) { if UIScreen.main.nativeBounds.width == 750 {//4.7inch self.SidewaysAlert = true self.SliderWidth = 657.7 }else if UIScreen.main.nativeBounds.width == 1125 {//5.4 5.8inch side self.SidewaysAlert = true self.SliderWidth = 438.5 }else if UIScreen.main.nativeBounds.width == 1242 {//5.5 6.5inch side self.SidewaysAlert = true self.SliderWidth = 326.4 }else if UIScreen.main.nativeBounds.width == 828 {//6.1inch side self.SidewaysAlert = true self.SliderWidth = 489.6 }else if UIScreen.main.nativeBounds.width == 1170 {//6.1inch side self.SidewaysAlert = true self.SliderWidth = 346.5 }else if UIScreen.main.nativeBounds.width == 1248 {//6.7inch side self.SidewaysAlert = true self.SliderWidth = 315.8 }else if UIScreen.main.nativeBounds.width == 768 {//7.9 9.7inch self.SliderWidth = 1142.4 }else if UIScreen.main.nativeBounds.width == 1536 {//7.9 9.7inch self.SliderWidth = 571.2 }else if UIScreen.main.nativeBounds.width == 1620 {//10.2inch self.SliderWidth = 541.6 }else if UIScreen.main.nativeBounds.width == 1668 {//10.5 11.0inch self.SliderWidth = 526 }else if UIScreen.main.nativeBounds.width == 1640 {//10.9inch self.SliderWidth = 535 }else if UIScreen.main.nativeBounds.width == 2048 {//12.9inch self.SliderWidth = 428.4 } } 実機にデモアプリを入れてサイズを確かめ、実機とシミュレータのサイズ比率から計算してみたのですが、実機で検証したもの以外はうまくいきません。 (edited)marchv/UIScreenExtension が持ってる値で試してみたらどうでしょうか。UIScreen.main.nativeBounds.width が同じ値でも端末によってppiは異なるので、そこで計算ずれてると思います。 omochiさんのいうようにまずはライブラリが持ってる値で試すと良さそうです。 (edited)pod 'SwiftyRSA', :podspec => "./SwiftyRSA.podspec" (edited) XCTAssertEqual(expected, actual) #define XCTAssertEqual(expression1, expression2, ...) となっており、特にどちらが期待する結果かは書かれていないのですがSwiftを書く人達の中での共通認識や慣習として期待する結果を先に書くというのがあるのでしょうか?XCTAssertEqual(expected, actual) と書かれていたのを見て、最初の引数がexpectedなのだな、と思っただけです。ちょっと調べても見つからなかったのでテストの中で統一されてたらどっちを期待する結果としてもいいのかもしれません。 XCTAssertEqual(table.rowCount, 0, "Row count was not zero.") XCTAssertEqual(table.columnCount, 0, "Column count was not zero.") と書かれているので XCTAssertEqual(実際の値, 期待する結果) がいいのかもしれません(Appleのドキュメントでそうなので)。UIPageViewControllerDataSource にある以下メソッドの呼び出しが余分に呼ばれたり、呼ばれなかったりする時があります。(初回はスワイプ 時はviewControllerBeforeとviewControllerAfterが同時に呼ばれる。以降で向きを切り替えてスワイプ した際の初回のスワイプ のイベントは呼ばれない) 但しtransitionStyleをpageControlに設定した際は正常な通知になるのですが、scrollに設定する際は他に設定する項目があるのでしょうか? 何かご存知の方がいらっしゃいましたらご教授頂けますと幸いです。 よろしくお願いいたします。 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
A server with the specified hostname could not be found のようにエラーが出てしまい、curl ではアクセスできるURLでもうまくクローリングできません。 何かネットワークの設定などが必要でしょうか? https://gist.github.com/dictav/52e180d8f449912a0c8fe77d53f61b1c URLSession を使っています。 session.dataTask(with: URLRequest(url: self.url)) { (data, response, error) in defer { self.isExecuting = false self.isFinished = true } if let err = error { print(self.url, err.localizedDescription) return } if let res = response as? HTTPURLResponse { print("status: \(res.statusCode)") } }.resume() (edited)httpMaximumConnectionsPerHost を設定しておかないとデフォルト 6 (macOS) らしいです https://developer.apple.com/documentation/foundation/urlsessionconfiguration/1407597-httpmaximumconnectionsperhostWarning unable to build chain to self-signed root for signer "(null)" 現状、他の開発メンバーと同じTeamに参加しているAppleアカウントでCertificateを作成し、そのCertificateをSelectしたProvisioning Profileもダウンロードした状態です。 Automatically Manage Signingにはチェック入れる/入れないどちらも試しましたが同じエラーが表示され、証明書の作り直しなども試しましたがビルドできませんでした。 原因や怪しいところがありましたら教えていただけると幸いです。@resultBuilder という機能を使うことで限定されたコンテキストにおいて if式、switch式相当のものを、それも構築される型を指定して組み立てることが出来ます。ざっくりどんなものかは、SwiftUIにおけるViewBuilderを参照してみて下さい。 (edited)Either.left(A) みたいに書く必要がある。Commonly Rejected Changes に挙げられていて、 それで解決できることは他の方法で解決できる ←これは同意だけど if式を導入すると大きなトレードオフがある ←実際どんなデメリットがあるのかはあまりよくわからないifexp みたいな高階関数を書いてちょっと楽しても良い ↓以下のlet初期化スタイルでもそんな困らない let a: Int if ... { a = 3 } else { a = 2 } と思っているval a = if (cond) { exp } else return (edited)val a = exp ?: return (edited)if ... { return A } ... return A みたいなものを表現しようとしたときにfunc ifx<T>(_ cond: Bool, then: () -> T, else: () -> T) -> T { if cond { return then() } else { return `else`() } } let a = ifx (1 + 1 > 1) { return 2 } else: { return 1 } print(a) // 2 今のSwiftだとこんなのが書けますね。if (foo) { return A } とだけ書いてある場合、このif式の型は Unit( voidみたいなもん) だね。ただ左辺が無いので意味はない。val a = if (foo) { 1 } else { 2 } // これはaへの代入 val b = if (foo) { return 1 } else { return 2 } // これはここからの脱出、bは代入されないCommonly Rejected Changes に挙げられていて、 それで解決できることは他の方法で解決できる ←これは同意だけど if式を導入すると大きなトレードオフがある ←実際どんなデメリットがあるのかはあまりよくわからない enum Either<R, L> { case right(R) case left(L) } @_functionBuilder struct Expression { public static func buildIf<C>(_ c: C?) -> C? { c } public static func buildEither<T, F>(first: T) -> Either<T, F> { .right(first) } public static func buildEither<T, F>(second: F) -> Either<T, F> { .left(second) } } func exp<T>(@Expression f: () -> T) -> T { f() } let a = exp { if true { "" } else { 1 } } print(type(of: a))Stack dump: 0. Program arguments: /usr/bin/swift -frontend -interpret - -disable-objc-interop -I /Libraries/.build/x86_64-unknown-linux-gnu/debug -I /Libraries/.build/checkouts/SwiftBacktrace/Sources/CSwiftBacktrace/include -I /Libraries/.build/checkouts/SwiftBacktrace/Sources/Clibunwind/include -I /Libraries/.build/checkouts/swift-nio/Sources/CNIOHTTPParser/include -I /Libraries/.build/checkouts/swift-nio/Sources/CNIOSHA1/include -I /Libraries/.build/checkouts/swift-nio/Sources/CNIOAtomics/include -I /Libraries/.build/checkouts/swift-nio/Sources/CNIOWindows/include -I /Libraries/.build/checkouts/swift-nio/Sources/CNIODarwin/include -I /Libraries/.build/checkouts/swift-nio/Sources/CNIOLinux/include -module-cache-path /Libraries/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -D DEBUG -Xcc -fmodule-map-file=/Libraries/.build/checkouts/SwiftBacktrace/Sources/CSwiftBacktrace/include/module.modulemap -Xcc -fmodule-map-file=/Libraries/.build/checkouts/SwiftBacktrace/Sources/Clibunwind/include/module.modulemap -Xcc -fmodule-map-file=/Libraries/.build/x86_64-unknown-linux-gnu/debug/CNIOHTTPParser.build/module.modulemap -Xcc -fmodule-map-file=/Libraries/.build/x86_64-unknown-linux-gnu/debug/CNIOSHA1.build/module.modulemap -Xcc -fmodule-map-file=/Libraries/.build/x86_64-unknown-linux-gnu/debug/CNIOAtomics.build/module.modulemap -Xcc -fmodule-map-file=/Libraries/.build/checkouts/swift-nio/Sources/CNIOWindows/include/module.modulemap -Xcc -fmodule-map-file=/Libraries/.build/x86_64-unknown-linux-gnu/debug/CNIODarwin.build/module.modulemap -Xcc -fmodule-map-file=/Libraries/.build/x86_64-unknown-linux-gnu/debug/CNIOLinux.build/module.modulemap -module-name main -lLibraries 1. Swift version 5.3.3 (swift-5.3.3-RELEASE) 2. While evaluating request TypeCheckSourceFileRequest(source_file "<stdin>") 3. While type-checking statement at [<stdin>:26:1 - line:32:1] RangeText="let a = exp { if true { "" } else { 1 } " 4. Whilextension ViewBuilder { public static func buildEither<TrueContent, FalseContent>(first: TrueContent) -> _ConditionalContent<TrueContent, FalseContent> where TrueContent : View, FalseContent : View public static func buildEither<TrueContent, FalseContent>(second: FalseContent) -> _ConditionalContent<TrueContent, FalseContent> where TrueContent : View, FalseContent : View }
https://yanamura.hatenablog.com/entry/2019/09/05/150849enum Either<R, L> { case right(R) case left(L) } @_functionBuilder struct Expression { static func buildBlock() -> () { return () } static func buildBlock<T1>(_ t1: T1) -> (T1) { return (t1) } public static func buildEither<T, F>(first: T) -> Either<T, F> { return .right(first) } public static func buildEither<T, F>(second: F) -> Either<T, F> { return .left(second) } } func exp<T>(@Expression f: () -> T) -> T { f() } let a = exp { if true { "" } else { 1 } } print(type(of: a))Either<String, Int>stderr:<stdin>:34:9: warning: will never be executed 1 ^ <stdin>:31:8: note: condition always evaluates to true if true { ^val a = if (cond) { exp } else return (edited)func calculatePaymentPrice(_ price : Int) -> Int{ if price < 0 { return 0 //エラーの代わり } return Int(Double(price)*1.1) } (edited)func calculatePaymentPrice(_ price : Int) -> Int{ if price < 0 { return 0 //エラーの代わり } return Int(Double(price)*1.1) } (edited)func calculatePaymentPrice(_ price : Int) -> Int { if price < 0 { 0 } else { Int(Double(price) * 1.1) } } こうなるって話をしてると思ってます。式1つなのでreturnは省略しました。func calculatePaymentPrice2(_ price : Int) -> Int { price < 0 ? 0 : Int(Double(price) * 1.1) } func calculatePaymentPrice3(_ price : Int) -> Int { ifx (price < 0) { 0 } else: { Int(Double(price) * 1.1) } } ↑この2つの書き方はできます。(ifxは自作の関数)func calculatePaymentPrice(_ price : Int) -> Int { if price < 0 { 0 } else { Int(Double(price) * 1.1) } } こうなるって話をしてると思ってます。式1つなのでreturnは省略しました。 func calculatePaymentPrice(_ price : Int) -> Int{ if price < 0 { return 0 //エラーの代わり } if price = 0 { return 0 //エラーの代わり } return Int(Double(price)*1.1) }func calculatePaymentPrice(_ price : Int) -> Int{ if price < 0 { 0 } else if price = 0 { 0 //エラーの代わり } else { Int(Double(price)*1.1) } } こうですか??if myArray.isEmpty { return -1 } if elseで表現することを強制される、ということはそれは即ちif式での早期脱出の数だけ、本文のネストが深くなるということであり、望ましいものではない。 2を採用した場合、if式の中でif式の値として早期脱出を表現することは不可能になる。 例 let a = if myCond { if myArray.isEmpty { return -1 } // この行は関数の返り値になり、aとして利用することは出来なくなる ... } これらの1,2双方の問題を解決しようとした場合、ラベル付returnのような複雑な仕様が必要となるが、それは望ましくない というのがSwiftでif/switch/for/whileが式として提供されない一般の理由、と考えて良いと思います。 個人的にはラベル付returnよりはResultBuilderの方がシンプルなんじゃないかな、という感想。 (edited)struct を使うことが多いです。 // イミュータブルクラス final class User { let age: Int = 0 } var user = User() // 年齢を 1 増やす user = User(age: user.age + 1)
// struct struct User { var age: Int = 0 } var user = User() // 年齢を 1 増やす user.age += 1
(edited)if 文の例で考えると、↓のような違いが生まれます。 // イミュータブルクラス user = if ... { // if 式がほしい User(age: user.age + 1) } else { User(age: user.age + 2) }
// struct if ... { // if 文で十分 user.age += 1 } else { user.age += 2 }if が式でなく文なのはミュータブルな値型( struct )を多用する Swift と馴染んでおり、適切な言語仕様ではないかと思います。 (edited)