天道酬勤,学无止境

swift

Swift associated types and protocol inheritance

Question I'm using Swift 2.2 and I declared a protocol with associated type as follow: protocol CollectionViewModeling { associatedtype CellType func cellAtIndexPath(indexPath: NSIndexPath) -> CellType } Now I have a view model protocol conform to the above protocol : enum MyItemCell { case MyItemCell1, MyItemCell2 } protocol ItemsListViewModeling: CollectionViewModeling { associatedtype CellType = MyCell } Finally, somewhere else, I want to declare a var that is conform to le protocol ItemsListViewModeling : var viewModel: ItemsListViewModeling And I'm getting this error : Protocol

2022-07-11 09:48:25    分类:技术分享    swift   mvvm   protocols   associated-types

Pause and Resume UIViewAnimation when app goes to background

Question I am animating a view and I want to pause it and resume it. Using an apple guide I created a CALayer Extension extension CALayer { func pause() { var pauseTime = self.convertTime(CACurrentMediaTime(), fromLayer: nil) self.speed = 0.0 self.timeOffset = pauseTime } func resume() { var pausedTime = self.timeOffset self.speed = 1.0 self.timeOffset = 0.0 self.beginTime = 0.0 var timeSincePause = self.convertTime(CACurrentMediaTime(), toLayer: nil) - pausedTime self.beginTime = timeSincePause } } This code is working perfectly except when that app goes to background. When I bring the App

2022-07-11 09:42:24    分类:技术分享    ios   swift   uiviewanimation

Check if an integer is divisible by another integer (Swift)

Question I need to check if an integer is divisible by another integer exactly. If not I would like to round it up to the closest multiple of the number. Example: var numberOne = 3 var numberTwo = 5 numberTwo is not a multiple of numberOne therefore I would like it to round numberTwo up to 6. How would I do this? Thank you Answer1 You can use the modulo operator %: numberTwo % numberOne == 0 The modulo finds the remainder of an integer division between 2 numbers, so for example: 20 / 3 = 6 20 % 3 = 20 - 6 * 3 = 2 The result of 20/3 is 6.666667 - the dividend (20) minus the integer part of that

2022-07-11 09:40:20    分类:技术分享    ios   swift   integer   division

Display Section Header UICollectionReusableView

Question I was working on iOS application and I have several problem about using UICollectionView cell. This time, I want to ask about how to display the section header of UICollectionView (UICollectionReusableView) I already implement the function like below : public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:

2022-05-17 05:26:07    分类:技术分享    ios   swift   xcode   uicollectionview   uicollectionreusableview

Get and Set Array in Swift Class

Question I was build an IOS application, and somehow I have to do an aggregation in swift class, but every time I want to get the data, it always return an error. it seems like I the data always return a nil result. I want to push the DoctorList member object (schedule) in the view controller class I have create the object, and I also called the init() function. but, however, when I push the (or call) the init function for the DoctorList class and pass the array of ScheduleList, the content of the schedule member in doctorlist class will always be empty. so when I try to get the schedule, it

2022-05-17 05:26:03    分类:技术分享    swift   class   oop

Reading a Variable that Exists in a Delegate Function

Question I have a delegate that modifies a variable called tagToIndex, and I am trying to return the modified variable from the delegate. tagToIndex is defined earlier on in the class, but it does not contain the information I need. My question is, is there any way to read the variable tagToIndex as it exists within the willPressItemAt function? As of right now, I have no way of reading the variable outside of the function. Obviously I cannot use the return command on willPressItemAt, because that would require I feed in parameters which I cannot do. Additionally, I tried to use delegates but

2022-05-17 05:20:22    分类:技术分享    ios   swift   delegates

Firebase queryEqual(toValue:

Question I am trying to using the queryEqual(toValue: in swift. The issue I'm having is that I am getting null. My data looks like this this "time" : { "149791341619654" : { "URL_ID" : "02D03DA8-A652-4EFA-946F-1F0F2A963449", "UUID" : "F496B98E-3B4E-4602-B06D-C09B7F42DCB5", "date" : 1.497913416196546E9, "huer" : 0, "latitude" : 32.87926715336076, "longitude" : -117.2139702738423, "type_" : "Image", "uid" : "BjN8usKaW7NhqD2DxPPqAkjhnSx2" }, "149791381894099" : { "URL_ID" : "AB8D20E9-D1EB-4FD0-B0A8-CE755D1D8B91", "UUID" : "F534E1FC-DC55-4AFB-847D-64B614EF7137", "date" : 1.497913818940991E9, "huer

2022-05-17 05:13:03    分类:技术分享    ios   swift   firebase   firebase-realtime-database

How to modify inputKeys for a custom CIFilter?

Question I am trying to create a custom CIFilter. So far this filter just brightens an image, its not what I actually want, but I'm doing it to see if I can get it working. Although the filter works with a set value of inputBlurRadius, I don't know how to change it so that the user can change the value. Here is the custom filter. import Foundation import UIKit public class CustomFilter: CIFilter { var inputImage: CIImage? private var kernel: CIColorKernel! var inputBrightness: CGFloat = 0.4 override public var outputImage: CIImage! { guard let inputImage = inputImage else { return nil } let

2022-05-17 04:30:04    分类:技术分享    swift

is it possible to get the index (String.Index) value from the cursor position of a UITextView element in Swift?

Question I'm looking to pull the index value (String.Index) from the user cursor position of a UITextView element. I'm using the selectedTextRange method to get the UITextRange value. How can I use that to retrieve the index value? Answer1 You can get the selected range offset from the beginning of your document to the start of the selected range and use that offset to get your string index as follow: extension UITextView { var cursorOffset: Int? { guard let range = selectedTextRange else { return nil } return offset(from: beginningOfDocument, to: range.start) } var cursorIndex: String.Index?

2022-05-17 04:04:03    分类:技术分享    ios   swift   xcode

PhotoPicker discovery error: Error Domain=PlugInKit Code=13

Question Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. I'm trying to display an image from the photo library in a UIImageView The full error is: 2017-06-09 21:55:59.063307+0200 firstapp2.0[12873:1120778] PhotoPicker discovery error: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled} My code is included below: import UIKit class ViewController: UIViewController,UIImagePickerControllerDelegate

2022-05-17 03:45:06    分类:技术分享    swift   uiimage   ios11