NSObject does not have a member named startIndex - ios

#IBAction func logEmButn(sender: AnyObject) {
let clonCountrLableItem = clonCountLable.text!
let baclCounterLableItem = baclCountLable.text!
let ironCountLableItem = ironCountLable.text!
let gymCountLableItem = gymCountLable.text!
let asp2CountLableItem = asp2CountLable.text!
let fiveHtpLableItem = fiveHtpLable.text!
let selectedLables = [clonCountLable, baclCountLable, ironCountLable, gymCountLable, asp2CountLable, fiveHtpLable]
for selectedLable in selectedLables {
if selectedLable != "0 s" {
println("You took \(selectedLable[selectedLable.startIndex])")
was working earlier today in a "similar" project, now it gives error
'NSObject' does not have a member named 'startIndex'
so, I tried:
println("You took \(selectedLable[selectedLable.description]) \(keyOfselectedLables)")
which gives a similar error, but ends with ... named 'subscript'
also have tried:
let lable = "rick"
for index in indices(lable) {
print("\(lable[index])")
}
which give error at runtime of : Could not cast value of type 'UILable' (0x199943e30) to 'NSString' (0x198fb2768).

See the below code, selectedLables is an array of UILabel's
let selectedLables = [clonCountLable, baclCountLable, ironCountLable, gymCountLable, asp2CountLable, fiveHtpLable]
but in the if statement you are equating selectedLable, of type UILabel, with a string constant.
for selectedLable in selectedLables {
if selectedLable != "0 s"
Since selectedLable is not a string type, it does not have a startIndex property or index.
So i think the array should be
let selectedLables = [baclCounterLableItem, ironCountLableItem, gymCountLableItem, asp2CountLableItem, fiveHtpLableItem]

Nitheesh George nailed it. (the last answer I see here.) Sorry if I messed up the flow here, total nube.

Related

FMDB / sqlite query problem with function

I am really new to swift and trying to understand ios programming. Basically I have a table where "id" and "fact" columns. id is primary key'd and here is the swift fuction I am trying to query the values
func getItemsFromRow(fact_cid: String) ->[FactsData]{
var rowData: [FactsData]!
let rawQ = "select * from facts where \(facts_id)=?"
if (openDatabase()){
do{
let results = try database.executeQuery(rawQ, value(forKey: fact_cid))
while results.next(){
let currentFact = FactsData(factid: results.string(forColumn: facts_id), factsdata: results.string(forColumn: facts_fld))
if rowData == nil {
rowData = [FactsData]()
}
rowData.append(currentFact)
}
}
}
return rowData
}
But this line gives me error
let results = try database.executeQuery(rawQ, value(forKey: fact_cid))
Error is
Cannot invoke 'executeQuery' with an argument list of type '(String, Any?)'
I am trying to pass the id as string. Not sure what I am doing wrong here.
Any help is much appreciated.
Change it to
let results = try database.executeQuery(rawQ, values: [fact_cid])
since the second parameter should be an array

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

I am new in Ios programming and the below expressing is giving an error:
let combine = date.enumerated().map {index, date in
return (date,self.arrFriendId[index],self.arrFriendName[index],self.arrFriendImage[index],self.arrMsgType[index],self.arrMessage[index], self.arrLastMsgTime[index], self.arrNotifyStatus[index])}
please help me to solve this.
thanks in advance
This error generally occurs when a single expression is doing a lot of things. So compiler tells you to break it to sub-expressions.
Assuming you want the output combine of type Array<Any>, You can do it like this:
let combine = date.enumerated().map { index, date -> Any in
let id = self.arrFriendId[index]
let name = self.arrFriendName[index]
let image = self.arrFriendImage[index]
let messageType = self.arrMsgType[index]
let message = self.arrMessage[index]
let messageTime = self.arrLastMsgTime[index]
let status = self.arrNotifyStatus[index]
return (date, id, name, image, messageType, message, messageTime, status)
}

strange error, returned value type different from the one in function body

I have XlsxReaderWritter Framework added to my app. I am having trouble with the first steps of using this framework, which is getting a worksheet in an excel file
Here is my code:
let file = BRAOfficeDocumentPackage.open(syllabusFileURL.relativePath)!
inputFile = file.workbook.worksheets[0] //this line does not compile and gives me this "Type [Any]! has no subscript members"
Alright so I cast it as the following:
inputFile = file.workbook.worksheets[0] as! BRAWorksheet
Now it compiles but I get a runtime error saying:
Could not cast value of type 'BRAWorksheet' (0x10df5fdc0) to 'BRAWorksheet' (0x10ce8e430).
So it seems the value is already BRAWorksheet and does not need casting, but as I said it wont compile saying its of type [Any]!
In the official documentation, here is how they get a worksheet;
#####Swift
//First worksheet in the workbook
var firstWorksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0]
//Worksheet named "Foo"
var fooWorksheet: BRAWorksheet = spreadsheet.workbook.createWorksheetNamed("Foo")
Since it is an imported framework, I checked the original Objective-C file for the function. It does not seem to return [Any]!. Here is the body:
- (NSArray *)worksheets {
NSMutableArray *worksheets = #[].mutableCopy;
for (BRASheet *sheet in _sheets) {
BRAWorksheet *worksheet = [self.relationships relationshipWithId:sheet.identifier];
worksheet.styles = _styles;
worksheet.sharedStrings = _sharedStrings;
worksheet.calcChain = _calcChain;
[worksheets addObject:worksheet];
}
return worksheets.count ? worksheets : nil;
}
So can Anyone tell me what the duck is going on??!
If You are using swift 3
let path: String = Bundle.main.path(forResource: "demo", ofType: "xlsx")!
let spreadsheet: BRAOfficeDocumentPackage = BRAOfficeDocumentPackage.open(path)
let worksheet: BRAWorksheet = spreadsheet.workbook.worksheets[0] as! BRAWorksheet
let sheet: BRASheet = spreadsheet.workbook.sheets[0] as! BRASheet
print(sheet.name) // print "Sheet1"
You should take care of the optional result:
var firstWorksheet: BRAWorksheet? = spreadsheet.workbook.worksheets?[0]

Swift 3.0 stringFromStringNumberOrNil replacement not found. App gives error "Ambiguous reference to member 'value' or Ambiguous use of 'value'"

In my previous app , we have used
let value = String.stringFromStringNumberOrNil(myProperty?.value)
valueLabel.text = value
In above code 'myProperty.value' was kind of 'Any' Class
When i am trying to convert the same code to swift 3.0 app cause error
I have wrote :
var value1 : Int = myProperty.value as Int
or
var value1 : String = aylaProperty.value as! String
App Gives error :
Ambiguous reference to member 'value'
What should i do for this ?
Use Strings initializer
let myPropertyValue:Any = 6
let x = myPropertyValue as! Int
let value = String(x)
let valueLabel = UILabel()
valueLabel.text = value

Unwrapping Optional Int in Swift

The API response I am working with returns a total amount. It supports multiple different currencies, so sometimes the currency symbol will be at the front (ex. $20.00) or at the end (ex. 20.00€). I am doing a check to see if the first char of the value is an int. In this specific case, the value "20.00€" is being returned. firstChar is "2" :
DOES NOT WORK:
let firstNumOpt: Int? = String(firstChar).toInt()
if let num = firstNumOpt { //20.00€
NSLog("Total: \(total)")
}
WORKS:
if let num = String(firstChar).toInt() { //20.00€
NSLog("Total: \(total)")
}
Can someone explain why the first code block does not work? Both ways seem identical to me.
Some debug info:
(lldb) po firstNumOpt
2
{
value = 2
}
(lldb) po num
411432864
Note: This isn't really an answer, but I needed to post code to describe what was working.
I'm not sure that your error is in the code that you've posted. Here is some code that I just ran in an empty playground:
func test(str: String) {
let firstNumOpt: Int? = String(str[str.startIndex]).toInt()
if let anum = firstNumOpt {
print("First worked on \(str)")
}
if let bnum = String(str[str.startIndex]).toInt() {
print("Second worked on \(str)")
}
}
test("20.00€") // prints "First worked on 20.00€" and "Second worked on 20.00€"
test("$20.00") // doesn't print anything

Resources