Post not yet marked as solved
I am trying to add an extension, that adds two functions to an array of FloatingPoint types.
I added to and extension to get a myDesibe var:
extension FloatingPoint {
public var myDescribe:String {
return String(format:"%.3f",self as! CVarArg)
}
}
This works fine (or seems to)
On the collection, I want to add two methods/vars.
One method returns a string with command delimited values:
public extension Array where Element: FloatingPoint {
var myDescribe:String {
var rvalue = ""
for (index,entry) in self.enumerated() {
var format = ",%@"
if (index == 0) {
format = "%@"
}
rvalue += String(format: format, entry.myDescribe)
}
return rvalue
}
}
That seems to be working fine.
The other I wan to pass the command delimited string in, and get an array of the type of elements of the Array. This does not work:
public extension Array where Element: FloatingPoint {
var myDescribe:String {
func fromDescription(desribe:String) -> [Element] {
var rvalue = [Element]()
for entry in desribe.components(separatedBy: ",") {
let trimmed = entry.trimmingCharacters(in: .whitespaces)
let temp = FloatLiteralType(trimmed) ?? FloatLiteralType.zero
// Now, how to convert this so I can append this in the array? This is the issue!!
rvalue.append(temp) // << THIS FAILS, as it wants an Int which I don't `understand`
}
return rvalue
}
}
All help is appreciated
Charles
Post not yet marked as solved
Does anyone have the format string to use to just get the 24 hour time (no am/pm) indicator from Date?
I can't seem to get 24 hour time with:
let now = Date()
now.formatted(date:.omitted,time:.complete) // Or any of the time options.
Post not yet marked as solved
I am trying to do a connection, with NWConneciton, and wanted to know if the connect fails? I can't seem to determine what status I should look for
Post not yet marked as solved
I have a NWConnection, that if an invalid (ip/port), is entered, I get in debug the error: nw_socket_handle_socket_event [C1.1.1:1] Socket SO_ERROR 61. But I can't seem to trap that error.
I have as my stateChangeHandler:
I am creating my connection:
let tcpOptions = NWProtocolTCP.Options()
tcpOptions.enableKeepalive = true
tcpOptions.keepaliveIdle = 2
tcpOptions.keepaliveCount = 2
tcpOptions.keepaliveInterval = 2
let params = NWParameters(tls: nil, tcp: tcpOptions)
nwConnection:NWConnection(host: NWEndpoint.Host(host), port: NWEndpoint.Port(port)!, using: params). (with known nonexistent ip/port).
I was hopping when I did a .start(), I would get an error in my state handler:
// ===============================================================================
func stateDidChange(to state: NWConnection.State) {
Swift.print("State change")
switch state {
case .waiting(let error):
print("Client waiting")
connectionDidFail(error: error)
case .ready:
print("Client connection ready")
case .failed(let error):
print("Client failed")
connectionDidFail(error: error)
case .preparing:
print("client preparing")
case .setup:
print("client setup")
case .cancelled:
print("client cancelled")
default:
print("Client unknown")
break
}
}
But it doesn't trap an error. So, where is this error coming from (I know the cause), but I want to trap it (in case a user puts in a wrong ip/port)
Post not yet marked as solved
I have some Objective C code, that I can not find the equivalent for Swift (many symbols are not present). It just lists all serial devices that match a prefix:
+ (NSArray*)serialDevicesWithPrefix:(NSString*)prefix {
NSMutableArray* modems=[NSMutableArray arrayWithCapacity:10];
kern_return_t kernResult;
mach_port_t masterPort ;
CFMutableDictionaryRef classesToMatch;
io_iterator_t matchingServices;
NSString* serialDevice=@"/dev/cu.";
serialDevice=[serialDevice stringByAppendingString:prefix];
// Lets find all serial device types ;
// Get the port for communications to kernal
kernResult = IOMainPort(kIOMainPortDefault, &masterPort) ;
if (kernResult == KERN_SUCCESS) {
// We got a good result!
classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue) ;
if (classesToMatch != NULL) {
// Now, we need to say we are looking for RS232 type of connections
// Each serial device object has a property with key
// kIOSerialBSDTypeKey and a value that is one of
// kIOSerialBSDAllTypes, kIOSerialBSDModemType,
// or kIOSerialBSDRS232Type. You can change the
// matching dictionary to find other types of serial
// devices by changing the last parameter in the above call
// to CFDictionarySetValue.
CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDRS232Type));
kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, &matchingServices);
if (kernResult == KERN_SUCCESS) {
// We did something!
// Matching serveres are the ones that "match" our need, now we have to loop through that and get the names
io_object_t modemService;
while ((modemService = IOIteratorNext(matchingServices))) {
CFTypeRef deviceFilePathAsCFString;
// Get the callout device's path (/dev/cu.xxxxx).
// The callout device should almost always be
// used. You would use the dialin device (/dev/tty.xxxxx) when
// monitoring a serial port for
// incoming calls, for example, a fax listener.
deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(modemService,CFSTR(kIOCalloutDeviceKey),kCFAllocatorDefault,0);
if (deviceFilePathAsCFString) {
// We got it!
NSString* currentModem=(__bridge_transfer NSString *)deviceFilePathAsCFString;
if ([currentModem hasPrefix:serialDevice]) {
[modems addObject:currentModem];
}
}
IOObjectRelease(modemService) ; // We need to release it!
}
// We should release our iterator
IOObjectRelease(matchingServices) ;
}
}
}
return modems ;
}
Does anyone have ideas?
Post not yet marked as solved
I have a value:UInt64 that I want to display as a "0x################". String(format"%x",value) will only accept a 32 bit unsigned int (and is verified by the documentation it references). There is no format specifier to display a 64 bit unsigned in hex format that I can find. What is the correct way to display a 64bit integer in hex format?
Post not yet marked as solved
I have seen examples of reading Ints from Data that use "withUnsafeBytes", but none with copyBytes.
I am trying to read an Int (and later different size Ints) from a Data.
Is there any example for Swift 5.5 , without going through a String ?
Post not yet marked as solved
I have a c++ project that is giving this warning:
Declaration requires an exit-time destructor
I understand the warning, as it is on a singleton class.
However, in the build options under Apple Clang-Warnings-C++ I have the following
Exit-Time C++ Destructors set to No
This is on Version 12.4 (12D4e) of Xcode and
macOS Big Sur Version 11.2.3 (20D91)
Is there another warning option I have to disable to eliminate this warning?