Posts

Post not yet marked as solved
0 Replies
576 Views
I have issue to request Authorization and get the total sleep hours from HealthKit func requestAuthorization(completion: @escaping (Bool) -> Void) {           let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!     let heartRate = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!     let spo2 = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.respiratoryRate)!     let sleepHours = HKObjectType.categoryType(forIdentifier: .sleepAnalysis)     let setType = Set(arrayLiteral: sleep_type_t) //error cannot find 'sleepType' in scope ****           guard let healthStore = self.healthStore else { return completion(false) }           healthStore.requestAuthorization(toShare: [], read: [stepType,heartRate,spo2,sleepHours]) { (success, error) in       completion(success)     }         }    func getSleepHours(completion: @escaping (_ sleepHours: String?) -> Void) {     let sampleType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!           // Get all samples from the last 24 hours     let endDate = Date()     let startDate = endDate.addingTimeInterval(-1.0 * 60.0 * 60.0 * 24.0)     let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])     let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)     // Sleep query     let sleepQuery = HKSampleQuery(sampleType: sampleType,predicate: predicate,limit: 0,sortDescriptors: [sortDescriptor]) { (query, results, error) -> Void in             if error != nil {return}             // Sum the sleep time             var minutesSleepAggr = 0.0             if let result = results {               for item in result {                 if let sample = item as? HKCategorySample {                   if sample.value == HKCategoryValueSleepAnalysis.asleep.rawValue && sample.startDate >= startDate {                       let sleepTime = sample.endDate.timeIntervalSince(sample.startDate)                       let minutesInAnHour = 60.0                       let minutesBetweenDates = sleepTime / minutesInAnHour                       minutesSleepAggr += minutesBetweenDates                   }                 }               }               let sleepHours = Double(String(format: "%.1f", minutesSleepAggr / 60))!               let sleepHours2 = "(sleepHours)"               completion(sleepHours2)               print("*** SLEEP HOURS: (String(describing: sleepHours))")             }       }               healthStore?.execute(sleepQuery)   }
Posted
by aulimge.
Last updated
.
Post not yet marked as solved
1 Replies
577 Views
//MARK : HealthStore   func getData() -> Double {     //Get Authorization     let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!     let heartRate = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate)!     let spo2 = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.respiratoryRate)!     healthStore.requestAuthorization(toShare: [], read: [stepType,heartRate,spo2]) { (chk, error) in       if (chk){         print("Permission Granted")                   var dumm = 0.0         //currentHeartRate(completion: nil)         self.getSteps(currentDate: Date()) { result in           globalString.todayStepsCount = result           dumm = result           print(result)         }         return dumm       }                    }         } //getData             func getSteps(currentDate: Date, completion: @escaping (Double) -> Void) {     guard let sampleType = HKCategoryType.quantityType(forIdentifier: .stepCount) else {       print("Get Steps not executed as is null")       return     }     let now = currentDate //Date()     let startDate = Calendar.current.startOfDay(for: now)     let predicate = HKQuery.predicateForSamples(withStart: startDate, end: now, options: .strictEndDate)     let query = HKStatisticsQuery(quantityType: sampleType, quantitySamplePredicate: predicate, options: .cumulativeSum)     { _, result, _ in         guard let result = result, let sum = result.sumQuantity() else {           completion(0.0)           return         }         completion(sum.doubleValue(for: HKUnit.count()))       }     healthStore.execute(query)   }
Posted
by aulimge.
Last updated
.
Post not yet marked as solved
1 Replies
2.5k Views
I have name the table view cell identifier as cell still keep crashing. Restart XCODE and restart MacBook several times still not working. ********** Codes****** extension ItemViewController: UICollectionViewDataSource, UICollectionViewDelegate {          func collectionView( collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {                  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ImageCollectionViewCell         if itemImages.count > 0 {             cell.setupImageWith(itemImage: itemImages[indexPath.row])         }         return cell     } ********** ********** Crash Error****** 2020-06-23 08:52:56.578332+0800 MarketPlaceDev[7650:1771105] * Assertion failure in -[UICollectionView dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3920.31.101/UICollectionView.m:5972 2020-06-23 08:52:56.579198+0800 MarketPlaceDev[7650:1771105] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' First throw call stack:
Posted
by aulimge.
Last updated
.
Post marked as solved
3 Replies
12k Views
I am using the new SwiftUI to fetch Data from Web Api and Displaying Using SwiftUI and encounter and error and could not compile.Error : Use of undeclared type BindableObject*******ContentView.swift*********import SwiftUIstruct ContentView: View { @ObjectBinding var model = PostListViewModel() var body: some View { List(model.posts) { post in Text(post.branchname) } }}struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() }}*******Post.swift*********import Foundationimport SwiftUIstruct Post : Codable, Hashable, Identifiable { //var id: ObjectIdentifier let id : Int let branchname : String let aboutus : String}*******PostListViewModel.swift*********import Foundationimport SwiftUIimport Combinefinal class PostListViewModel: BindableObject { init() { fetchPosts() } var posts = [Post]() { didSet { didChange.send(self) } } private func fetchPosts() { Webservice().getAllPosts { self.posts = $0 } } let didChange = PassthroughSubject<PostListViewModel,Never>() }
Posted
by aulimge.
Last updated
.