0

Ok, so I'm good with php and vba, but now I'm trying to learn iOS apps. Im attempting to write one that calculates the shift schedule for a rotating shift. Ive got the function down... or so i think. Im trying to test it, but how do i call it?

shiftCalc.crew("days","2015-09-13");

here is my code..

class shiftCalc
{
var startDate: String = "2002-11-12";
var cycleOrder: String = "45321";
var tweekLength: Int = 4;
var tweekStart: Int = 27;
var midsStart: Int = 9;
var swingsStart: Int = 0;
var daysStart: Int = 18;
let dateFormatter = NSDateFormatter();

// Returns what crew is on shift for given shift and date
func crew(shift: String, strInputDate: String) -> String
{
    var intOffset: Int;
    var intShiftStart: Int;
    var intCrewWeek: Int;
    var crew: String;
    dateFormatter.dateFormat = "yyyy-MM-dd";
    let varDate = dateFormatter.dateFromString(startDate);
    let inputDate = dateFormatter.dateFromString(strInputDate)

    if(inputDate!.compare(varDate!) == NSComparisonResult.OrderedDescending)
    {
        NSLog("Date Before Shift Schedules Implemented");
        // Exit Script
    }
    else
    {
        //Get number of days between input date and start date
        intOffset = daysBetweenDate(varDate!, endDate: inputDate!);
    }
    switch shift
    {
    case "swings":
        intShiftStart = swingsStart;
    case "mids":
        intShiftStart = midsStart;
    case "days":
        intShiftStart = daysStart;
    case "tweek":
        intShiftStart = tweekStart;
    default:
        // Not a valid shift
        return "";
    }
    intOffset = daysBetweenDate(varDate!, endDate: inputDate!);
    intCrewWeek = 1 + (((intOffset + 35 - intShiftStart) % 35)/7);
    crew = String(Array(cycleOrder.characters)[intCrewWeek]);
    return crew;
}
func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int
{
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: []);
    return components.day
}
}

But Xcode is telling me i am passing too many parameters. "Extra argument in call"

So please critique me, i am learning all of this so what did i do wrong?

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
brandenwagner
  • 138
  • 1
  • 5
  • 20
  • I would probably not name the variable the same name as the function: "crew". MirekE's answer is how you call the function. – rholmes Sep 13 '15 at 16:26
  • That makes sense. I fixed that. I had that originally from when i tried to write this using vba style. – brandenwagner Sep 13 '15 at 16:30

2 Answers2

0

Class names should start with uppercase letters. So once you fix it, your code could look like this:

let shiftCalc = ShiftCalc()
shiftCalc.crew("days", strInputDate: "2015-09-13")
MirekE
  • 11,515
  • 5
  • 35
  • 28
  • This fixed it. Thank you, i know understand how to call it properly in swift. So when calling a function with multiple inputs do i always need to specify the variable name in the call? strInputDate:? – brandenwagner Sep 13 '15 at 16:31
0

If you want to call the function on the class you have to declare the function as class function

class func crew(shift: String, strInputDate: String) -> String

In Swift you have always pass the second and subsequent parameter names inside a class.

shiftCalc.crew("days", strInputDate:"2015-09-13")

And you don't need any semicolon

vadian
  • 274,689
  • 30
  • 353
  • 361