The issue is considerably exhausting to know since I am looking for a strategy to fulfill the grammar necessities of a overseas language in my app (Russian, on this case). I attempted to seek out solutions on the Russian Stackoverflow, however it’s useless and has at all times been like that. It is gonna be a protracted learn however bear with me, please, as I need individuals to know what I am doing and why there is no totally different answer aside from exhausting coding a bunch of logical workarounds.
The setup is as follows:
- I’ve two view controllers (FirstVC and SecondVC).
- Every view controller has a desk.
- As soon as the consumer faucets on a cell within the FirstVC, the content material of that cell passes on to the desk header view within the SecondVC.
- BUT: The textual content within the desk header view of the SecondVC ought to account for Russian declensions (one of many the reason why Russian is so exhausting).
- The desk header view of the SecondVC has two strings sitting on prime of one another. The highest one says “СПИСОК” which interprets as “LIST”. It’s static and shall be perpetually left as is. The underside one says “участников” (interprets as “of contributors of the”) adopted by a string interpolation. Altogether it ought to learn as “LIST of contributors of the” adopted by no matter occasion title is inherited from the FirstVC’s desk cell.
That is the exhausting half. In English, you may put phrases collectively most often, and there you go, you might have a grammatically appropriate sentence. However Russian is bizarre on this sense, as it’s a must to modify the phrases themselves to account for declensions (the closest analogy I might consider is if you use singulars and plurals. You possibly can’t say: “I had two bottle of water”. You would need to add “s” to “bottle”. One thing like that.
Fortunately, the phrases I would like to control are often the primary ones within the string. Extra hardly ever, although, one may need to take care of the mixture of the primary and the second phrase within the string.
Because of one in all @Leo Dabus’s solutions on Stackoverflow, I managed to get the primary phrase out of the string, change it after which add to the remainder of the phrases in that string. Now I would like a strategy to get the primary two phrases out of the string to handle these uncommon instances talked about above. How do I do this utilizing the identical logic?
This is the code for the SecondVC:
import UIKit
class SecondVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var names: [Cell2Model] = []
var textForChanging: String = ""
var switchedFirstWord: String {
swap textForChanging.firstWord {
case "Соревнование": return "соревнования" //interprets as "competitors"
default: return "none" } }
var restoOfTheName: String {
var string = textForChanging
string.enumerateSubstrings(in: string.startIndex..., choices: .byWords) { (_, _, enclosingRange, cease) in
string.removeSubrange(enclosingRange)
cease = true }
return string }
var adjustedEventNameText: String {
return switchedFirstWord + " " + restoOfTheName }
override func viewDidLoad() {
tremendous.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// names shall be taken from UITextFields on the fourth view controller
names = [
(Cell2Model(name: "Participant 1")),
(Cell2Model(name: "Participant 2")),
(Cell2Model(name: "Participant 3")) ]
let tableHeader = UIView()
tableView.tableHeaderView = tableHeader
let labelOne = UILabel(body: tableHeader.bounds)
labelOne.textual content = "СПИСОК"
labelOne.font = UIFont(title:"Arial-BoldMT", measurement: 17.0)
labelOne.numberOfLines = 0
labelOne.lineBreakMode = .byWordWrapping
labelOne.textAlignment = .middle
labelOne.translatesAutoresizingMaskIntoConstraints = false
let labelTwo = UILabel(body: tableHeader.bounds)
labelTwo.translatesAutoresizingMaskIntoConstraints = false
labelTwo.textual content = "участников (adjustedEventNameText)"
labelTwo.font = UIFont(title:"Arial-BoldMT", measurement: 17.0)
labelTwo.numberOfLines = 0
labelTwo.lineBreakMode = .byWordWrapping
labelTwo.textAlignment = .middle
labelTwo.translatesAutoresizingMaskIntoConstraints = false
tableHeader.addSubview(labelOne)
tableHeader.addSubview(labelTwo)
let g = tableHeader.layoutMarginsGuide
NSLayoutConstraint.activate([
labelOne.topAnchor.constraint(equalTo: g.topAnchor, constant: 11.0),
labelOne.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
labelOne.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
labelTwo.topAnchor.constraint(equalTo: labelOne.bottomAnchor, constant: 0.0),
labelTwo.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
labelTwo.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0), ])
let c = labelTwo.bottomAnchor.constraint(equalTo: g.bottomAnchor, fixed: -11.0)
c.precedence = .required - 1
c.isActive = true
}
override func viewDidLayoutSubviews() {
tremendous.viewDidLayoutSubviews()
if let tableHeader = tableView.tableHeaderView {
let fitSize: CGSize = CGSize(width: tableView.body.width, peak: .greatestFiniteMagnitude)
let sz: CGSize = tableHeader.systemLayoutSizeFitting(fitSize, withHorizontalFittingPriority: .required, verticalFittingPriority: .defaultLow)
let peak: CGFloat = sz.peak
var headerFrame = tableHeader.body
if peak != headerFrame.measurement.peak {
headerFrame.measurement.peak = peak
tableHeader.body = headerFrame
tableView.tableHeaderView = tableHeader } } }
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
return names.depend }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "secondCell", for: indexPath) as! SecondCell
let Identify = names[indexPath.row]
cell.label.textual content = Identify.title
return cell } }
extension StringProtocol {
var byWords: [SubSequence] { elements(separated: .byWords) }
func elements(separated choices: String.EnumerationOptions)-> [SubSequence] {
var elements: [SubSequence] = []
enumerateSubstrings(in: startIndex..., choices: choices) { _, vary, _, _ in elements.append(self[range]) }
return elements }
var firstWord: SubSequence? {
var phrase: SubSequence?
enumerateSubstrings(in: startIndex..., choices: .byWords) { _, vary, _, cease in
phrase = self[range]
cease = true }
return phrase } }
This is the code for the FirstVC in the event you want it:
import UIKit
class FirstVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var names: [Cell1Model] = []
override func viewDidLoad() {
tremendous.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// names shall be taken from UITextFields on the third view controller
names = [
Cell1Model(name: "Соревнование по легкой атлетике"), // translates as "athletics competition (or "competition on athletics" to put it into the Russian words order)
Cell1Model(name: "Благотворительное соревнование по легкой атлетике") // translates as "athletics charity competition" (or "charity competition on athletics" in the Russian words order)
]
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let vc = storyboard?.instantiateViewController(withIdentifier: "SecondVC") as? SecondVC {
vc.textForChanging = names[indexPath.row].title!
self.navigationController?.pushViewController(vc, animated: true) } }
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
return names.depend }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
let Identify = names[indexPath.row]
cell.label.textual content = Identify.title
return cell }
Thanks prematurely!