Error converting "Swift invokes UIDatePicker from UITextField" to swift3

Asked 2 years ago, Updated 2 years ago, 108 views

When I try to use Qiita's "Swift to call UIDatePicker from UITextField to prepare the toolbar for completion button etc" in swift3, I get the following two errors:Could you tell me how to improve it?
1.34th line

//UIToolBar Configuration
toolBar=UIToolbar(frame:***CGRectMake***(0,self.view.frame.size.height/6,self.view.frame.size.width, 40.0))

CGRectMake to CGRect

Argument labels do not match any....

and error.How do I fix this?

2. Press // "Today" to set today's date line 84 is modified in Fixit Replace

***let components: NSDateComponents=calender.components (NSCalendar.Unit.YearCalendarUnit | NSCalendar.Unit.MonthCalendarUnit | NSCalendar.Unit.DayCalendarUnit | NSCalendar.Unit.HourCalendarUnit | NSCalendar.Unit.Unit.Unit.Mineral.UnCalendar.UnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitaryUnitary

is

No' | 'candidates produce the expected contextual result type 'NSCalendarUnit'

and error.How do I fix it?

http://qiita.com/matsuhisa_h/items/4bb9803828efb89e0305

xcode swift3

2022-09-30 16:29

1 Answers

"If you are writing questions, please make sure to include as much ""necessary and sufficient information as possible to reproduce the situation"" (as text)."Posting a link is useful as an auxiliary information, but if you lose a link in the future, your question will become meaningless (to future readers).

"When I read your question, it seems that ""copy the linked code as it is, and only where the auto-collect Suggestion is displayed from Xcode is correct."""

By the way, that's not enough to make the linked code work properly. I hope you got the other corrections right, but I can't say anything about it because I don't have the necessary information as I wrote above.

(1) CGRectMake

*If you want to see the conclusion soon, skip to the conclusion section.If you want to improve your development skills, I'd like you to look through everything, including the link.

In Swift3, many of the functions for instance generation were deleted.Most of them can be replaced by constructors.The function in the title is for generating CGRect, so you're saying that there should be a replacement within the CGRect constructor.

CGRect

 init (origin:CGPoint, size:CGSize)

Create a rectangle with the specified origin and size.

 init (x: Double, y: Double, width: Double, height: Double)

Create a rectangle with coordinates and dimensions specified as
floating-point values.

 init (x:Int, y:Int, width:Int, height:Int)

Create a rectangle with coordinates and dimensions specified as
integer values.

 init(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat)

Create a rectangle with coordinates and dimensions specified as
CGFloat values.

Even if you don't understand the English explanation, if you know that CGRectMake is a function that takes four parameters of type CGFloat, you will find that you can use the last init(x:CGFloat,y:CGFloat,width: CGFloat).

In conclusion,This is how you rewrite it.

 toolBar=UIToolbar (frame: CGRect (x:0, y:self.view.frame.size.height/6, width:self.view.frame.size.width, height:40.0))

(2) NSCalendar.Unit

*In the end, NSCalendar.Unit is not required, so if you want to see a conclusion soon, please skip it to Further modification

NSCalendar.Unit

The NSCalendarUnit type, which used to be just an integer value, is now imported as the NSCalendar.Unit type of OptionSet, and the type of notation is similar to the previous | type.

Using Swift with Cocoa and Objective-C (Swift 3.0.1) Foundation.

Swift3 adds a value-type data type that corresponds to several reference data types.If you take an excerpt from this code...

NSCalendar Calendar
NSDateComponents DateComponents
NSDate Date
NSLocale Locale

(NSDateFormatter may be like ?, but this is something else, just NS was taken from the class name.)

While current Swift does not often seem to aggressively digest modifications to these data types, it is recommended that you use the full value type if you want to make it as far as possible for the future.

To put that into perspective, the linked dateToString method looks like this…

func dateToString(date:Date)->String{
    // Type name omitted where Swift never makes a mistake in type inference
    // OptionSet and enum constants can omit the type name portion in the appropriate location (Calendar.Identifier.gregorian->.gregorian)
    let calendar = Calendar (identifier: .gregorian)
    // OptionSet uses constant literal for `Set` style, and the name part of the type is optional.
    let comps = calendar.dateComponents ([.year, .month, .day, .hour, .minute, .second, .weekday], from:date)
    
    // DateFormatter is a reference type, so let is appropriate
    let date_formatter=DateFormatter()
    // `weekdays` is a very bad way to write `String?`(=`Options<String>`) and you should use `let` because you don't change the content.
    let weeks: Array = [nil, "Sun", "Mon", "Tuesday", "Wednesday", "Thursday", "Friday", "Sat"]
    
    date_formatter.locale=Locale(identifier: "ja")
    date_formatter.dateFormat="Yyyy MM month dd day (\(weekdays[comps.weekday!])"
    
    return date_formatter.string (from:date)
}

After writing the code, I realized that if you convert the value of the Date type into a string with the code above, a strange value that you don't understand will be filled in the part where you wanted to include the day of the week.

You don't have to have your own array of weekdays to put in a very standard day of the week name, and you don't have to use the NSDateComponents that you've struggled with.

(The date of the article is from the Swift 2.1 era, but this is a bug that occurs even if you use Swift at that time, so it means that it contains code that has not been verified enough.)

Additional modifications will produce this code.

func dateToString(date:Date)->String{
    // DateFormatter is a reference type, so let is appropriate
    let date_formatter=DateFormatter()
    // If you want to use Japanese Locale to write a single character for the day of the week, you don't need an array of weekdays.
    
    date_formatter.locale=Locale(identifier: "ja")
    date_formatter.dateFormat="yyyyy MM month dd day (E)"//<-`E` is the format character for the day of the week output.
    
    return date_formatter.string (from:date)
}

*If you use the above code, you will need to make some corrections to other parts related to it.

The Swift information on the Internet is flooded with codes that seem to have not been properly verified, such as "No matter how much Swift 2 is, there will be no way to write this."(I don't recommend this, where does it come from, and it doesn't work at all...) Please develop your sense enough to judge whether this is really the right way.

Also, if you look for it carefully, you may find that the same author himself has already published the Swift3 version of the article.If you're developing with Swift3, you'd better start by looking up articles for Swift3 (even if it's a little bit of time), and you won't have to worry about unnecessary things.

As I wrote at the beginning, this is not enough to make the linked article code work, but if there are any questions or errors in this answer, please let me know in a separate thread if there are any comments that you cannot make work.


2022-09-30 16:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.