Swift date(byAdding:to:) returns nil for trivial calculation in REPL
This is a bug in the Swift REPL, see
- SR-11593 REPL incorrectly reports URL as nil
- SR-12172 Description of Date? expression is printed as nil even when non-nil
It affects both optional and non-optional values of some Swift overlay types (such as Date
, Calendar
, URL
). Example:
$ swift
Welcome to Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15).
Type :help for assistance.
1> import Foundation
2>
3> let d1 = Date()
d1: Date = {}
4> let d2: Date? = Date()
d2: Date? = nil
5> let url = URL(string: "https://www.google.com")
url: URL? = nil
As a workaround you can print the values:
7> print(d1)
2020-03-03 10:17:00 +0000
8> print(d2)
Optional(2020-03-03 10:17:07 +0000)
9> print(url)
Optional(https://www.google.com)
Swift: DateFormatter returning nil
After a bit of playing with the debugger, I figured out why this is.
The reason why this happens is because it's Date
. If you change your method to return an NSDate
, the debugger shows it correctly.
From what I observed: This behaviour happens for the group of types that This happens for most of the types on this page that are also in Date
, and others such as TimeZone
belong to. That is, Swift types in Foundation
that have an Objective-C counterpart, but isn't a "direct port" like DateFormatter
/NSDateFormatter
. I can't really describe it well, but it's basically the types in Foundation
where their documentation page doesn't let you select an Objective-C version on the top right.Foundation
. Exceptions are:
AffineTransform
Data
DateInterval
Measurement
Notification
If you use another debugger command, po
, you'll see that it displays Date
s correctly. So what's the difference between print
and po
?
Running help print
and help po
can tell us that:
print
:
Evaluate an expression on the current thread. Displays any returned value
with LLDB's default formatting. Expects 'raw' input (see 'help
raw-input'.)
Syntax: print <expr>
Command Options Usage:
print <expr>
'print' is an abbreviation for 'expression --'
po
:
Evaluate an expression on the current thread. Displays any returned value
with formatting controlled by the type's author. Expects 'raw' input (see
'help raw-input'.)
Syntax: po <expr>
Command Options Usage:
po <expr>
'po' is an abbreviation for 'expression -O --'
Notice how for po
it says "with formatting controlled by the type's author". And if you type help expression
, you will see what the -O
option for expression
does:
Display using a language-specific description API, if possible.
So it is possible to conclude that po
is more "specific to Swift" than print
, hence it is able to print out things like Date
and TimeZone
s, because print
simply doesn't know how to print a Swift Date
.
Date from Calendar.dateComponents returning nil in Swift
If you want to strip off the time portion of a date (set it to midnight), then you can use Calendar startOfDay
:
let date = Calendar.current.startOfDay(for: Date())
This will give you midnight local time for the current date.
If you want midnight of the current date for a different timezone, create a new Calendar
instance and set its timeZone
as needed.
Calculate number of weeks in a given month - Swift
Your code computes the number of weeks which occur (complete or partially)
in a month. What you apparently want is the number of Mondays
in the given month. With NSDateComponents
and in particular with
the weekdayOrdinal
property you can compute the first
(weekdayOrdinal=1
) and last (weekdayOrdinal=-1
) Monday
in a month. Then compute the difference in weeks (and add one).
A possible implementation in Swift 2:
func numberOfMondaysInMonth(month: Int, forYear year: Int) -> Int?
{
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
calendar.firstWeekday = 2 // 2 == Monday
// First monday in month:
let comps = NSDateComponents()
comps.month = month
comps.year = year
comps.weekday = calendar.firstWeekday
comps.weekdayOrdinal = 1
guard let first = calendar.dateFromComponents(comps) else {
return nil
}
// Last monday in month:
comps.weekdayOrdinal = -1
guard let last = calendar.dateFromComponents(comps) else {
return nil
}
// Difference in weeks:
let weeks = calendar.components(.WeekOfMonth, fromDate: first, toDate: last, options: [])
return weeks.weekOfMonth + 1
}
Note: That a negative weekdayOrdinal
counts backwards from the end of the month is not apparent form the documentation. It was observed in
Determine NSDate for Memorial Day in a given year and confirmed by Dave DeLong).
Update for Swift 3:
func numberOfMondaysInMonth(_ month: Int, forYear year: Int) -> Int? {
var calendar = Calendar(identifier: .gregorian)
calendar.firstWeekday = 2 // 2 == Monday
// First monday in month:
var comps = DateComponents(year: year, month: month,
weekday: calendar.firstWeekday, weekdayOrdinal: 1)
guard let first = calendar.date(from: comps) else {
return nil
}
// Last monday in month:
comps.weekdayOrdinal = -1
guard let last = calendar.date(from: comps) else {
return nil
}
// Difference in weeks:
let weeks = calendar.dateComponents([.weekOfMonth], from: first, to: last)
return weeks.weekOfMonth! + 1
}
Null-coalescing assignment operator in Swift 3
First, make the operator generic instead of using Any
:
infix operator ??= : AssignmentPrecedence
func ??=<T>(lhs: inout T?, rhs: @autoclosure () -> T?) {
if lhs != nil { return }
lhs = rhs()
}
Second, the left operand needs to be an optional (otherwise it
could not be tested against nil
):
var x: Int? = 3
x ??= 7
Set the maximum character length of a UITextField
While the UITextField
class has no max length property, it's relatively simple to get this functionality by setting the text field's delegate
and implementing the following delegate method:
Objective-C
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 25;
}
Swift
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.count ?? 0
if range.length + range.location > currentCharacterCount {
return false
}
let newLength = currentCharacterCount + string.count - range.length
return newLength <= 25
}
Before the text field changes, the UITextField asks the delegate if the specified text should be changed. The text field has not changed at this point, so we grab it's current length and the string length we're inserting (either through pasting copied text or typing a single character using the keyboard), minus the range length. If this value is too long (more than 25 characters in this example), return NO
to prohibit the change.
When typing in a single character at the end of a text field, the range.location
will be the current field's length, and range.length
will be 0 because we're not replacing/deleting anything. Inserting into the middle of a text field just means a different range.location
, and pasting multiple characters just means string
has more than one character in it.
Deleting single characters or cutting multiple characters is specified by a range
with a non-zero length, and an empty string. Replacement is just a range deletion with a non-empty string.
A note on the crashing "undo" bug
As is mentioned in the comments, there is a bug with UITextField
that can lead to a crash.
If you paste in to the field, but the paste is prevented by your validation implementation, the paste operation is still recorded in the application's undo buffer. If you then fire an undo (by shaking the device and confirming an Undo), the UITextField
will attempt to replace the string it thinks it pasted in to itself with an empty string. This will crash because it never actually pasted the string in to itself. It will try to replace a part of the string that doesn't exist.
Fortunately you can protect the UITextField
from killing itself like this. You just need to ensure that the range it proposes to replace does exist within its current string. This is what the initial sanity check above does.
swift 3.0 with copy and paste working fine.
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let str = (textView.text + text)
if str.characters.count <= 10 {
return true
}
textView.text = str.substring(to: str.index(str.startIndex, offsetBy: 10))
return false
}
Hope it's helpful to you.
Related Topics
Self' Used Before All Stored Properties Are Initialized
Swift Equivalent to _Attribute((Objc_Requires_Super))
How to Set a New Root View Controller
How to Connect to Self Signed Servers Using Alamofire 1.3
Using Some Protocol as a Concrete Type Conforming to Another Protocol Is Not Supported
Alamofire - Nsurlcache Is Not Working
Use Resources in Unit Tests with Swift Package Manager
Adding Swift File to New View Controller in Xcode? (Easy)
Order of Modifiers in Swiftui View Impacts View Appearance
Swift: Can Someone Explain This Syntax 'Numbers.Sort { $0 > $1 }' for Me
Scrollview + Navigationview Animation Glitch Swiftui
Conditional Property in Swiftui
Do Capture Lists of Inner Closures Need to Redeclare 'Self' as 'Weak' or 'Unowned'
How to Create a PDF in Swift with Cocoa (Mac)
Swift: Overriding == in Subclass Results Invocation of == in Superclass Only