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())
Related
I got this statement in Swift code which produces an error when executing in playground:
let colors: [String: [Float]] = ["skyBlue" : [240.0/255.0, 248.0/255.0, 255.0/255.0,1.0],
"cWhite" : [250.0/255.0, 250.0/255.0, 250.0/255.0, 1.0]]
The error is : expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
Then I changed the arrays element type to Double which just works fine.
However I am asking myself why this happens ?
As I said using Double it works just fine. So my guess is that Swift tries to guess the type and therefore Double works better in this example than Float.
Similar issues have been reported before, and (as I understand it) the problem is the automatic type inference for "complicated" expressions. You should file a bug report at Apple.
It compiles with a dictionary of one color, but not with two.
In this concrete case, you can work around it by converting each number in the array to a Float explicitly:
let colors = [
"skyBlue" : [Float(240.0/255.0), Float(248.0/255.0), Float(255.0/255.0),Float(1.0)],
"cWhite" : [Float(250.0/255.0), Float(250.0/255.0), Float(250.0/255.0), Float(1.0)]
]
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
}
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"
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))
Whilst debugging in Xcode_3.1.2 I am pretty sure I could see the contents of my NSString arrays. However after upgrading to 3.2 I only see the following ...
I know I can print the object in (gdb) using "po planetArray" or simply click in the debugger and "print description to console" I am just curious, as I am sure it worked prior to upgrading. Anyone know anything about this?
cheers gary
edit: data formatters is on and it shows what you see above ...
This is because GDB acts as if the variable you are viewing is out of scope while it really just is confused about what each part function or method call of the data formatter is returning (the data formatter is the "{(unichar *)Xcode_CFStringSummary($VAR, $ID)}:s" part you are seeing.
When you are debugging and you are in a method where you know a local variable must be in scope right now, open the debugger window and the area where you can see "Variable", "Value" and "Summary" column titles double click the "Summary" row entry for the variable you are interested in and enter the following (for array types like NSArray or NSCFArray):
"{(int)[$VAR count]} objects {(NSString *)[(NSArray *)$VAR description]}:s"
then press return. You have now overwritten the default data formatter provided by Xcode's GDB extension (to be found in various plists at "/Developer/Library/Xcode/CustomDataViews/") with your own data formatter string.
Your own overrides are saved at "~/Library/Application Support/Developer/Shared/Xcode/CustomDataViews/CustomDataViews.plist" and if you want to have the Apple default data formatter back just double click the row for a variable of the same type and delete whatever is there.
The nitty-gritty details: In the custom expression above the "{}" construct tells GDB to execute a command (as if you where executing it from GDB's debugger command line, which means the same restrictions apply: you need to specify the return type in cast parens in front of every function or method which returns something). The ":s" behind the closing curly brace tells Xcode and GDB to reference the "Summary" column. Also valid would be ":v" which references the "Value" column which most of the time is just the pointer value. Everything that is outside of the curly braces is shown verbatim.
Unfortuntely curly braces can't be nested which invalidates ternary operator conditionals.
So with the above data formatter you should see the following for an empty NSArray:
"0 objects (\n)"
If you want to write your own data formatters as GDB extensions (equivalent to specifying a function akin to Xcode_CFStringSummary above) you can do so. Take a look at the following header: "/Developer/Applications/Xcode.app/Contents/PlugIns/GDBMIDebugging.xcplugin/Contents/Headers/DataFormatterPlugin.h"
it will tell you all you need to know. But it can be hard to get it right. It might be easier and less error prone to just define another method on your class and call that from the data formatter string instead of "description".
In the Run > Variables View menu in Xcode, is "Use Data Formatters" enabled?
I am not sure if this helps but if you select the array value to wish to see in the debugger window and the go to the Menu : Run > Variables View > View Variable As
you can change it from "NSCFString *" to "NSString *". You then see the value so "Planet_1" for example.
Cheers,
Kevin