How do I set an NSTableColumn headerCell stringValue in swift? - macos

The documentation makes it look as if it would be as easy as this:
var tc = NSTableColumn(identifier: "mycolumn")
tc.headerCell.stStringValue("foo")
The last line of code is a compile error, which I don't understand. On top of that I get a couple of different compile error message for this exact same line of code depending on what mood XCode seems to be in. I've seen the following compile errors:
AnyObject does not have a member named 'stStringValue'
Cannot convert the expression's type 'NSString' to type 'StringLiteralConvertible'
I get this when try setting a variable and putting that in there:
Cannot convert the expression's type 'String' to type 'String?!'
I get this when I try the "foo \(bar)" variant:
Could not find member 'convertFromStringInterpolationSegment'
Hilariously enough if I declare a variable with type String?! it says it Cannot convert the expression's type 'String?!' to type 'String?!'
I don't understand what is going on. Is this a bug?
If I try this in a playground, the playground actually wants to autocomplete with an extra argument, this format:
tc.headerCell.setStringValue("foo", resolvingEntities: false)
This has no compile time errors, but results in a seg fault at run time. This format is also not in the documentation.
I really have no idea what's going on and the error messages aren't helping.

I finally got this to work, despite the terrible error messages. The problem is that headerCell is typed as AnyObject, I had to cast it. The second problem was that in swift land it's a property, not a method.
This finally worked:
var tc = NSTableColumn(identifier: "mycolumn")
let hc = column1.headerCell as NSTableHeaderCell
hc.stringValue = "foo"

Related

Generic parameter "Element" could not be inferred -- Swift

So I'm in an Xcode playground, trying to remove the last character from a string. I think it should be really simple, but my code isn't working.
let test = "Get rid of that L"
Array(test.characters).dropLast().joinWithSeparator("")
I get an error on the second line "Generic parameter "Element" could not be inferred"
Don't need the join bit, just do this:
let lastCharRemoved = String(test.characters.dropLast())

greenline Xcode EXC_BAD_INSTRUCTION on NSUserDefaults Optional

I've been writing an app that involves using NSUserDefaults to store a few Int variables and it's been working fine. I thought I was finished and was doing some final testing and one of the first lines of code that I wrote, and that has been working consistently before, has failed me.
Apparently the green line error is supposed to occur if I try to unwrap an optional that has a value of nil, but this variable is still very much an optional
var savedTotalSeconds: Int? = userDefaults.objectForKey("totalSecondsKey") as Int?
Why would this possibly return an error? It was working fine before and I only changed things I thought were unrelated to it. In the app I have a button to remove this stored value via:
userDefaults.removeObjectForKey("totalSecondsKey")
What could possibly have gone wrong?
Try using 'as? Int' instead of 'as Int?'
The difference is that the first one tries, and might fail, at casting to Int. That failure will be captured in the optionality of the resulting variable.
The second one tries to coerce the object to 'Int?'.

Viewing Swift optional values in debugger

Trying to get my feet wet in writing and debugging Swift code, I wrote the following. This is on OS X 10.10.2, Xcode 6.2.
let text : String? = "This is some text.\nJust for fun."
let lines : [String]? =
text?.componentsSeparatedByCharactersInSet(
NSCharacterSet.newlineCharacterSet())
println("\(lines)")
Breaking on the println ... line, the debugger's variable view shows Some as the value of lines. In my understanding, this indicates that lines is of an optional type and contains a value wrapped in Some, as opposed to being None.
Knowing that, how can I use the debugger to inspect what that value actually is? I tried:
clicking the small "i" button, which produces the output "Some" in the debugger console.
entering po lines and po lines! in the debugger console, which yields EXC_BAD_ACCESS. Update: Following a fresh installation of Xcode, po lines seems to have no effect, i.e. no output.
The final line prints Optional(["This is some text.", "Just for fun."]), which is exactly what I'd like the debugger to show.
How can I get at the unwrapped string array value of lines, and what is the difference between the optional variable lines, whose unwrapped value the debugger doesn't show, and the optional variable text, whose value it does show? Will I actually have to introduce an auxiliary variable every time I want to debug an optional value, or use printf-debugging?
Maybe I am misunderstanding, but you made "lines" an optional value. Because of that, you would need to unwrap it in your println statement or not make it an optional value. Take off the "?" after your "lines" declaration first and see if that works for you.
let lines : [String] =
To print an optional value use:
if let unwrappedLines = lines { // only executes if lines is not nil
println("\(unwrappedLines)") // prints the unwrapped variable
}

Array and Dictionary type declarations in Swift

According to my understanding of the documentation, this should be correct:
var cookies: [NSHTTPCookie] = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as [NSHTTPCookie]
where I'm creating an array of NSHTTPCookie objects. The interpreter does not like this syntax, however, giving me "Expected type after 'as'" and putting a little pointer at the opening bracket of the [NSHTTPCookie] at the end.
However, this works:
var cookies:NSHTTPCookie[] = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as NSHTTPCookie[]
From the documentation, it seems like the first version is more correct, however.
Here's another example, this time with someone else's code. No one else using this code has reported the same behavior I get. (This is just a snippet; if the context is relevant let me know and I'll post more)
func asDict(x: AnyObject) -> [String:AnyObject]? {
return x as? [String:AnyObject]
}
In this case the playground interpreter objects in both places [String:AnyObject] is used. It just doesn't seem to be recognizing it as a type.
I double-checked to make sure I have the most recent beta of Xcode 6, but it seems much more likely to me that the problem is in my understanding rather than in the tool, since this would be a mighty big bug for only me to experience.
You must be using an old beta, this works in Beta 5 playground:
import Foundation
println("hello")
var cookies:[NSHTTPCookie] = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as [NSHTTPCookie]
println("goodbye")

Type mismatch error while reading lotus notes document in vb6

Am trying to read the lotus notes document using VB6.I can able to read the values of the but suddenly type mismatch error is throwed.When i reintialise the vb6 variable it works but stops after certain point.
ex; address field in lotus notes
lsaddress=ImsField(doc.address)
private function ImsField(pValue)
ImsField=pValue(0)
end function
Like this I am reading the remaining fields but at certain point the runtime error "13" type mismatch error throwed.
I have to manually reintialize by
set doc=view.getdocumentbykey(doclist)
The type mismatch error occurs for a certain field. The issue should be a data type incompatibility. Try to figure out which field causes the error.
Use GetItemValue() instead of short notation for accessing fields and don't use ImsField():
lsaddress=doc.GetItemValue("address")(0)
The type mismatch is occurring because you are encountering a case where pValue is not an array. That will occur when you attempt to reference a NotesItem that does not exist. I.e., doc.MissingItem.
You should not use the shorthand notation doc.itemName. It is convenient, but it leads to sloppy coding. You should use getItemValue as everyone else is suggesting, and also you should check to see if the NotesItem exists. I.e.,
if doc.hasItem("myItem") then
lsaddress=doc.getItemValue("myItem")(0)
end if
Notes and Domino are schema-less. There are no data integrity checks other than what you write yourself. You may think that the item always has to be there, but the truth is that there is nothing that will ever guarantee that, so it is always up to you to write your code so that it doesn't assume anything.
BTW: There are other checks that you might want to perform besides just whether or not the field exists. You might want to check the field's type as well, but to do that requires going one more level up the object chain and using getFirstItem instead of getItemValue, which I'm not going to get into here. And the reason, once again, is that Notes and Domino are schema-less. You might think that a given item must always be a text list, but all it takes is someone writing sloppy code in an one-time fix-it agent and you could end up having a document in which that item is numeric!
Checking your fields is actually a good reason (sometimes) to encapsulate your field access in a function, much like the way you have attempted to do. The reason I added "sometimes" above is that your code's behavior for a missing field isn't necessarily always going to be the same, but for cases where you just want to return a default value when the field doesn't exist you can use something like this:
lsaddress ImsField("address","")
private function ImsField(fieldName,defaultValue)
if doc.hasItem(fieldName) then
lsaddress=doc.getItemValue(fieldName)(0)
else
lsaddress=defaultValue
end if
end function
Type mismatch comes,
When you try to set values from one kind of datatype variable to different datatype of another variable.
Eg:-
dim x as String
Dim z as variant
z= Split("Test:XXX",":")
x=z
The above will through the error what you mentioned.
So check the below code...
lsaddress = ImsField(doc.address)
What is the datatype of lsaddress?
What is the return type of ImsField(doc.address)?
If the above function parameter is a string, then you should pass the parameter like (doc.address(0))

Resources