Swift HKCategorySample - swift

How do I create an HKCategorySample? I'm following the docs but it errors with
Cannot invoke 'init' with an argument list of type '(type: HKCategoryType, value: Int, startDate: NSDate, endDate: NSDate)'
The docs seem to indicate that those are the correct argument types to pass it, so what is it actually complaining about?
The code I'm using is:
let sample = HKCategorySample(
type: HKCategoryTypeIdentifierSleepAnalysis as HKCategoryType,
value: HKCategoryValueSleepAnalysis.Asleep as Int,
startDate: start,
endDate: end)
where both start and end are NSDates

You have 2 problems in your code:
HKCategoryTypeIdentifierSleepAnalysis is just a identifier String, you must instantiate it with HKObjectType.categoryTypeForIdentifier()
HKCategoryValueSleepAnalysis is enum, you cannot cast it to Int. To extract Int from it, you have to use .rawValue property.
Try:
let sample = HKCategorySample(
type: HKObjectType.categoryTypeForIdentifier(HKCategoryTypeIdentifierSleepAnalysis),
value: HKCategoryValueSleepAnalysis.Asleep.rawValue,
startDate: start,
endDate: end
)

Related

SwiftUI return type Error: Cannot convert return expression of type 'Foundation.Date' to return type 'BarCharts.Date'

I am doing a bar chart in my SwiftUI app, however I encountered a problem...
struct ViewMonth: Identifiable {
let id = UUID()
let date: Date
let viewCount: Int
}
I defined several variable types in the above code.
struct Date {
static func from(year: Int, month: Int, day: Int) -> Date {
let components = DateComponents(year: year, month: month, day: day)
return Calendar.current.date(from: components)!
}
}
It seems like I can't convert the expression type 'Foundation.Date' to 'BarCharts.Date'. I don't understand the error message. Please help!
I expect the code will yield no errors.
The error complains about a terminology conflict of your type Date with existing Date in Foundation.
Basically there are three options:
Replace struct Date with extension Date.
Rename your struct Date as something which does not interfere with any type in the Foundation framework.
Declare all instances of your custom type as BarCharts.Date.
I recommend the first option.
There are two types: Foundation.Date is the date type that everyone uses. And I suppose you defined your struct Date inside a class Barcharts, so it is a Barcharts.Date.
The “from” function is declared to return Date - which is a shortcut for Barcharts.Date. Now the error message should make sense. It’s up to you to decide what you wanted it to return.

Convert UUID to String representation eg: 1816 to Cycling Speed And Cadence

I'm connecting to a Cycling Speed and Cadence service via bluetooth and am saving the peripheral into an array (for tableview).
I have implemented this struct
struct BTPeripheral: Identifiable {
let id: Int
let name: String
let uuid: UUID
let desc: String
}
and am calling is as
discoveredBTDevices.append(BTPeripheral.init(
id: discoveredBTDevices.count,
name: peripheral.name!,
uuid: peripheral.identifier,
desc: service.uuid.uuidstring) // trying service.uuid results in error
)
which results in this output
id: 0,
name: "Wahoo CADENCE 1DE8",
uuid: E98F3843-1C6A-E4DE-6EF3-76B013950007,
desc: "1816"
The description is the string 1816 (which is the UUID of the cycle Speed & Cad service)
let serviceCyclingSpeedAndCadenceUUID = CBUUID(string: "1816")
How can I convert the 1816 into the string "Cycling Speed and Cadence" which is what gets printed out
print(service.uuid) // this results in "cycling Speed and Cadence"
As noted above, putting service.uuid results in an error Cannot convert value of type 'CBUUID' to expected argument type 'String'
You can just get its description (which, IIRC, is what print will do eventually, as CBUUID conforms to CustomStringConvertible):
service.uuid.description
Or use a string interpolation:
"\(service.uuid)"
debugDescription produces the same result too, but I wouldn't use this unless this is actually for debugging.

How do I define a hasura gql mutation that has an argument type defined in another schema?

Here is my GQL... (note the variable $rrule)
mutation CREATE(
$title: String!,
$description: String!,
$duration: interval!,
$photo_url: String,
$rrule: String!,
$venue_id: Int!
) {
result:insert_event_templates_one(
object: {
title: $title,
description: $description,
duration: $duration,
photo_url: $photo_url,
rrule: $rrule,
venue_id: $venue_id
}
) {
id
}
}
rrule is a custom column type in another schema: _rrule
It can an implicit cast defined as follows:
CREATE CAST (TEXT AS _rrule.RRULE)
WITH FUNCTION _rrule.rrule(TEXT)
AS IMPLICIT;
How do I define my mutation to reference that cast? Right now when I run this mutation I get the following error:
variable rrule of type String! is used in position expecting rrule
So Hasura seems to know the underlying column type, but can't use its implicit cast?
The error does not have anything to do with the underlying datasource. The argument where the $rrule variable is being used accepts a GraphQL type named rrule. A variable can only be passed to an argument if its type matches. So the type of $rrule must be the same as the type of the argument rrule -- that is, it's type should also be rrule.
mutation CREATE(
$rrule: rrule!
...
) {
...
}

Swift cannot create array of tuples containing a nested type

I'm trying to do something that should be possible, but I'm getting a strange error. I have a nested type Gravl.Node and am trying to instantiate an array of tuples like so:
var attributes = [(attribute: String?, value: Gravl.Node)]()
…but Swift is complaining:
Cannot call value of non-function type '[(attribute: String?.Type, value: Gravl.Node.Type)]'
Any idea what gives? If I replace the nested type with something else it works fine.
Is this a bug in the parser/compiler?
Yes, this is a bug as noted by this question.
Following the example of that Q & A, you can change the syntax of the way you create the array:
var attributes: [(attribute: String?, value: Gravl.Node)] = []
or
var attributes = Array<(attribute: String?, value: Gravl.Node)>()
In this case, you can also work around the issue by creating a typealias:
Example:
class Gravl {
enum Node {
case first, last
}
}
typealias Gravl_Node = Gravl.Node // work around Swift bug
var attributes = [(attribute: String?, value: Gravl_Node)]()
attributes.append((attribute: "hello", value: Gravl.Node.first))

NSNumber & NSDate (swift 3)

I'm trying to create a timeStamp for sent/received messages to store inside of my database (firebase) and I'm not sure if I'm going about it correctly. Here's the line of code thats giving me an error:
Previously, I would write:
let timeStamp: NSNumber = Int(NSDate().timeIntervalSince1970))
but I'm getting the same error:
"Argument labels '(_:)' do not match any available overloads"
You can't assign an Int value to an NSNumber variable. You need to create an NSNumber from the Int. And you need to specify the parameter label:
let timeStamp: NSNumber = NSNumber(value: Int(NSDate().timeIntervalSince1970)))
Of course you now don't need to specifically state the data type:
let timeStamp = NSNumber(value: Int(NSDate().timeIntervalSince1970)))
timestamp: NSDate().timeIntervalSince1970 as NSNumber