I am messing around with Mach-O Headers and dyld and came across this function _dyld_get_image_header that returns a pointer to the mach header of a dynamic library.
I thought I might be able to access the Mach-O segments of dynamic libraries through this pointer following this article. However, I am not able to access the properties of the Mach-O Headers and am getting errors . In the Swift code below, I attempted to access the ncmds or load command property of the struct and was met with an error:
var currentLibrary = 0
// Gets Pointer to Mach Header of First Dynamic Library, Index 0
let libHeader = unsafeBitCast(_dyld_get_image_header(UInt32(currentLibrary)), to: UnsafePointer<mach_header_64>.self)
print(libHeader.ncmds)
Error:
Value of type 'UnsafePointer<mach_header_64>' has no member 'ncmds'
It's similar to c, where you would use the -> operator (libHeader->ncmds), requivalent to (*libHeader).ncmds. That is, first your reference the pointer to get to the mach_header_64 value, then you access its ncmds field.
In Swift, it's the exact same idea, with different syntax: libHeaver.pointee.ncmds
Related
I created a Phone struct in swift and initialized it. After initializing it I made a method called chargePhone which checks if the phone is less than 90 battery and charge it by 10 if it isn't. When I tried using the method for a object i created i ran into an error and I'm not sure how to fix it.
The problem is you are trying to access aPhone in the same scope as where it is defined
But there are other problems.
First aPhone's type isn't Phone so you can't access aPhone.chargePhone even if they are in different scope
Second , If you want to access aPhone.chargePhone , aPhone must be like
let aPhone = Phone(b:75 , c : "Blue"....)
even if you will get error.
Structures are value type so you can't make such a definition in struct . You must change it with class
When using Swift 3, I was defining my model like so
class Model: NSObject {
var prop1: String
}
When I wanted to access the static string value of the property name prop1, I would use let sad = #keyPath(Model.prop1) and it would give me "prop1" printed out. Happy days.
The problem is, that since upgrading to Swift 4, I am having trouble doing the above. I see in other posts that we can use the new \Model.prop1 syntax but that seems to be providing the value of property rather than the string representation of the name.
I am also refactoring out the need for NSObject on my Swift models, but I would have thought I can still get this functionality.
Any help here would be appreciated!
Swift properties do not necessarily retain the strings of the property names at runtime. Therefore, if the Swift key path syntax were able to give you this string value, it would only be able to be used on NSObject-derived classes. The Swift key path syntax doesn't only work with those, though; it can also be used to refer to properties of non-#objc classes and structs. Therefore, this is not possible. The #keyPath syntax remains available, however, to get the string key path of an Objective-C property.
I recently switched to Swift 3 and I got an error with the following line that I didn't get in swift 2. The layerClient call refers to the layerkit api, but the error seems to deal more with typing than the api. The error itself is "Expression Type 'Set' is ambiguous without more ".
layerClient.autodownloadMIMETypes = Set<NSObject>(arrayLiteral: "image/png")
I assume you're using this framework.
You don't need the <NSObject> when creating the Set. It can figure out with type it contains by the parameter you pass to the init method. Also autodownloadMIMETypes type in swift would be Set<String> which wouldnt match Set<NSObject>. This should work.
layerClient.autodownloadMIMETypes = Set(arrayLiteral: "image/png")
Also, since Set conforms to the ExpressibleByArrayLiteral protocol, you should be able to just create it like an array.
layerClient.autodownloadMIMETypes = ["image/png"]
I am transferring data from my iOS app to its Watch extension via the application context. I want to send a custom object I've created (named WeatherReport).
let context = ["report" : WeatherReport]
WCSession.defaultSession().updateApplicationContext(context)
However, I get the following error:
Value of type WeatherReport does not conform toe expected dictionary
value type 'AnyObject'
I am wondering why I am unable to set my custom object as a value in the dictionary I am trying to pass as the applicationContext.
Even if you could get past the compiler error you would get a runtime error. WCSession dictionaries can only contain property list types, which are just basic types such as strings, numbers, data, etc.
If you really want to send your custom object you'll have to serialize it first. The better solution is likely to convert your object in to a plist dictionary (each property becomes a key-value in the dictionary).
In my device driver I use
write_Parport_data(struct parport *p, unsigned char data);
In my application, I have to create variable of type struct parport and assign the first element to a value.
such as:
struct parport strApp;
strApp.base = 0x378; // Statement 1
Then:
write_Parport_data(&strApp, 0xff);
gcc compiler error about statement 1
error: invalid use of undefined type 'struct parport'
Even if I include in my application #include
I get the same error.
Even if I use insmod, the mknod to make the driver ready, still compiling the application will give the same error. How my application should use struct parport, what is missing?
Thanks
Are you sure you are including linux/include/linux/parport.h? And giving the compiler the path in the compilation? In other words, are there other instances of defining a var with the parport structure that does not get a compiler error?
Where you have defined this structure ? You should include that header.
You should include that file which has definition of your struct parport
If you have defined that structure in C file, then you should extern that struct definition in header file and include that header file.