I'm a beginner to Swift and I'm trying to run Swift 2.3 code in Xcode 8.0
var result: SecTrustResultType = 0
SecTrustEvaluate(trust, &result)
Error message: Cannot convert value of type 'int' to specified type 'SecTrustResultType'
I asked this question before and it was marked a duplicate, but the link to the similar question didn't exactly help me.
Cannot convert value of type 'int' to specified type 'SecTrustResultType'
SecTrustResultType is likely an enum. Check the Swift language book (freely available on the AppStore) to see how you initialise an enum. That's a basic language feature that you must learn, so it's pointless to tell you for this specific case.
Related
I am looking some help because I've tried to recover some picture with that code for a recycler View:
int images [ ] = {
R.drawable.lower_legs; R.drawable.upper_legs; R.drawable.tronc
R.drawable.back; R.drawable.arm_and_shoulder
}
but two errors appears : the first one is : unresolved reference with my int, and the second one is Variable expected. I would to recover these pictures with that code in my adapter :
holder.muscleImage.setImageResource(images[position])
How can I have a right code ? Thanks all !
val images = listOf(
R.drawable.lower_legs, R.drawable.upper_legs, R.drawable.tronc,
R.drawable.back, R.drawable.arm_and_shoulder
)
I don't want to extrapolate without knowing more context, but it could be argued that the syntax problem you are having is due to not knowing the basic Kotlin syntax.
Variable expected
Everything has to be, a file, a class, a variable or a function. So when you are assigning int images [] = ... you are not doing anything because the correct way to assign a variable is starting the declaration with the reserved word val.
unresolved reference with my int
In Kotlin the type is Int, a lower case might be caused by a Java confusion. And the type you need there is List<Int>. So by saying int you would be trying to assign a single number to a collection of numbers.
As a general recommendation learning new languages just by jumping on a project can be hard, taking an afternoon to read the basic syntax smooths the learning curve.
For Kotlin I would strongly recommend doing the Koans also TutorialPoints is short and straight to the point.
I have an error that used to look like this in Objective-C
NSString * const JKConfigurationErrorDomain;
typedef NS_ENUM(NSInteger, JKConfigurationCode) {
JKConfigurationCodeUnknown,
JKConfigurationCodeSomethingBad,
JKConfigurationCodeParsing,
};
Now, this is ugly to use in Swift. But since Swift 4, we can use NSErrorDomain and NS_ERROR_ENUM to make the imported error much nicer in Swift:
NSErrorDomain const JKConfigurationErrorDomain;
typedef NS_ERROR_ENUM(JKConfigurationErrorDomain, JKConfigurationCode) {
JKConfigurationCodeUnknown,
JKConfigurationErrorSomethingBad,
JKConfigurationErrorParsing,
};
This means I can now do stuff in Swift like this:
if let myError = error as? JKConfigurationError, myError.code = .somethingBad {
// handle it
}
instead of having to cast error to NSError, then check its .domain then look at the .code which is an integer, etc.
So far, so good. But my library is called JKConfiguration and there is already a JKConfiguration object (the center piece of the library) in there and as soon as I start using JKConfiguration anywhere in the library code I get an error:
'JKConfiguration' is ambiguous for type lookup in this context
I don't get it, why? What does NSErrorDomain or NS_ERROR_ENUM do such that the type lookup becomes ambiguous and how can I fix it?
What I tried already:
use NS_SWIFT_NAME on the NS_ERROR_ENUM typedef and rename it to something else. Looking at the generated Swift header, the rename works, but doesn't solve the issue
Change the name of the error domain (and thus of the generated error type in Swift). Seems to work according to the generated Swift header, but the issue still persists. Why is that?
The issue is not, as I initially thought, in the name of the error domain. Nor is it a problem with the library name. It’s a problem of the error enum‘s name, in the example above: JKConfigurationCode.
What the Compiler does for the enum cases of an NS_ERROR_ENUM is two-fold:
use the name of the enum and remove that prefix from all enum cases before importing them to swift
create an enum with the given name to hold those cases. If the given name ends with Code remove that suffix.
So that last part is the issue. It means that NS_ERROR_ENUM(AnyDomainName, JKConfigurationCode) generates an enum in Swift to hold the error codes with the name JKConfiguration (without the Code) prefix. But that type already exists in my example, which leads to the ambiguity.
So the solution is to change
NS_ERROR_ENUM(JKConfigurationErrorDomain, JKConfigurationCode)
to
NS_ERROR_ENUM(JKConfigurationErrorDomain, JKConfigurationSomethingCode)
Or similar.
Don’t forget to update all the prefixes of your enum cases though, as it seems the compiler won’t find them if the prefixes don’t match the enum name.
Why doesn’t NS_SWIFT_NAME work to rename the enum?
Best I can tell, NS_SWIFT_NAME causes the type to be renamed but not the cases. This leads to an empty type (Swift chooses a struct in that case) being generated for the error code as Swift doesn’t seem to find the cases. And the original container for the enum cases still has the offending name.
As you seen in this picture below, for some reason my DirectCast wont except ANYTHING for the second argument. It says it requires a type, but, it won't take any object at all!
Thanks for any help! I'm using VB.net so all .net answers are acceptable :)
EDIT
Ok, so apparently I'm not giving it the right kind of type. Could somebody please clarify this? Assuming the type it needs to cast to is gridElement, what should I replace objType with?
DirectCast requires an object prototype (i.e. just giving it the intended class name) rather than a System.Type descriptor object. To cast an object using a System.Type, you will want to utilize CTypeDynamic():
Return CTypeDynamic(createElementByIdAndLayer.MemberwiseClone(), objType)
The error is essentially telling you a class with the type name "objType" does not exist.
Its expecting a "Type", not a "Type Object".
What is the return value of the function?
I'm trying register presenters with Windsor using the convention based method but trying to do this in VB.NET, but the problem is it does not want to compile this statement:
Dim type = GetType(AbstractPresenter(Of))
I am getting : Too few type arguments to AbstractPresenter(Of TView, TPresenter)
Which I don't understand because this is a valid statement according to question. Also showing valid in other C# to VB.NET converters when converting typeof(AbstractPresenter<>).
Any ideas?
There are two type arguments, and you need to specify this, just as you would do for multi-dimensional arrays:
Dim type = GetType(AbstractPresenter(Of ,))
Looks weird, but now the compiler knows that AbstractPresenter expects two type arguments.
By the way, C# has the same requirement. So the above would be written as:
var type = typeof(AbstractPresenter<,>);
Im trying to use the ComAutomationFactory class in VB .NET the example im working from is c# and is working fine but my project doesnt compile and says this class is not defined??
also what is the return type? in the c# example it returns a type of dynamic but this type does not exist in vb .NET?
Possibly you're sample is old, according to this blog:
http://silverlight-essentials.blogspot.com/2010/03/breaking-changes-in-com-interop-between.html
It's changed name as:
The class ComAutomationFactory has been renamed to AutomationFactory.
Additionally you have to reference System.Runtime.InteropServices.Automation for this class.
Regarding the return type, the answer to this question has a solution:
Iterating over Word Document Fields using ComAutomationFactory in Silverlight 4