天道酬勤,学无止境

swift

BAD_ACCESS during recursive calls in Swift

While toying with Swift, I've encountered a situation which crashes and I still not have figured out why. Let define: class TestClass { var iteration: Int = 0 func tick() -> Void{ if iteration > 100000 { print("Done!") return } iteration++ tick() } } The tick() function calls itself and each time increments iteration. Any call of the type let test = TestClass() test.tick() makes the program crash after a rather small number of recursions (around 50000 on my iMac), with a EXC_BAD_ACCESS error: If I define a similar struct instead of a class, there is no crash (at least not in these limits)

2021-07-13 17:31:21    分类:问答    swift   recursion

How to call completionHandler for performFetchWithCompletionHandler in Swift

how can I call the completion Handler for the background fetch in Swift. I do the following: func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { // Do something completionHandler (UIBackgroundFetchResultNoData) // This does not work :( return } Can you please help me? Thanks, Tobi

2021-07-13 17:15:22    分类:问答    swift   uiapplication

Firebase Cloud Messaging Notifications for iOS not showing opened and analytics

I'am using FCM to send notifications to iOS apps. The notifications are received on the device well, but I can not get the "Opened" or "Conversion " rate in the Notification console GUI. It always shows "0 opened" and "0 Conversion" for almost 200 0000 notifications sent. I double checked all the implementation but I can't get it working. I have FirebaseAppDelegateProxyEnabled set to YES in plist. I implemented all the methods listed on Firebase guides and docs. I don't have any notification related events showing in the Firebase analytics pane either.

2021-07-13 17:10:47    分类:问答    ios   swift   firebase   firebase-cloud-messaging   swizzling

Apple watch and iPhone are not connected when the app in phone goes to background

I ran my iPhone app and its watch extension in the simulator. I send messages from phone to watch and vice versa. The messages are sent and received without any issue. But when I send the iPhone app to background, and then a message is sent from watch to iPhone, I continuously get the following log: [WC] __33-[WCXPCManager onqueue_reconnect]_block_invoke error reconnecting to daemon due to NSXPCConnectionInterrupted I searched regarding this but did not find any proper solution. Many have posted it as an issue in the betas of Xcode. I am using the latest Xcode 8.1. What could be the proper

2021-07-13 16:55:33    分类:问答    swift   watchkit   apple-watch   watchos-2

How to check if a variable is nil

I have a variable var a: [AnyObject? -> Void] and I am adding data in to it by append method. Now I want to check if the variable is nil or not. I tried using [] but not working and also tried "", this also not working, can anyone tell what is the meaning of this variable and how to check if it is nil.

2021-07-13 16:52:48    分类:问答    swift

Cannot invoke initializer for type with no arguments - Swift

I'm moving from objective-c to swift and all i'm trying to do is simply create an instance of class so that I can access a property on said class. var myClassInstance = MyClass() print("length is \(myClassInstance.variableOne)") something in that regard but I'm getting an error Cannot invoke initializer for type 'MyClass' with no arguments

2021-07-13 16:22:58    分类:问答    ios   swift

CIAdditionCompositing giving incorrect effect

I am trying to create an image by averaging several other images. To achieve this, I first darken each image by a factor equivalent to the number of images I am averaging: func darkenImage(by multiplier: CGFloat) -> CIImage? { let divImage = CIImage(color: CIColor(red: multiplier, green: multiplier, blue: multiplier)) let divImageResized = divImage.cropped(to: self.extent) //Set multiplier image to same size as image to be darkened if let divFilter = CIFilter(name: "CIMultiplyBlendMode", parameters: ["inputImage":self, "inputBackgroundImage":divImageResized]) { return divFilter.outputImage }

2021-07-13 15:57:31    分类:问答    ios   swift   macos   ciimage

SwiftUI how to adjust different screen sizes

I'm using SwiftUI to develop a people list page, the iPhone X screen is big enough but the titles are out of the screen in iPhone 8: iPhone X: However in iPhone 8 or smaller screen the "Find People" is too close to left and "Follow All" is even out of screen: I know in UIKit autolayout will solve this very easy but I wonder what's the best way or proper way for SwiftUI to solve this, some answer saying using like Spacer or HStack, but none of them actually work. var body: some View { NavigationView { List { ForEach(people) {person in PersonView(person: person) } }.navigationBarItems(leading

2021-07-13 15:36:44    分类:问答    ios   swift   swiftui

Why is the protocol function not being called

I have a file that implements the protocol and another file that calls the // // DeviceBLE.swift // import Foundation import CoreBlueTooth import QuartzCore import UIKit class DeviceBLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { var centralManager : CBCentralManager! init() { super.init() centralManager = CBCentralManager(delegate: self, queue: nil) } func centralManagerDidUpdateState(central: CBCentralManager!){ // Determine the state of the peripheral if (central.state == .PoweredOff) { println("CoreBluetooth BLE hardware is powered off") } else if (central.state ==

2021-07-13 15:32:28    分类:问答    swift

Scroll UITextView to specific text

i have a uitextview with large texts and above i have a search field. Anything searched, i would love the uitextview to scroll to that specific text and hightlight. And i need to make sure that the uitextview scrolls in such a way that the searched text stays in the first line I populated the textview with attributed text with some attributes. What i tried is get the text before the target text and tried to get the size of it adding the exact attributes i added on textview text at the beginning. made a CGPoint using that height and set the contentOffset of the uitextview. But the textview

2021-07-13 14:41:22    分类:问答    ios   swift   uitextview   nsattributedstring