I'm trying to find a way to remove instances with names as empty strings from my array that receives the data. how do I modify my filter to do that.
self.cPlayerArr = fillPlayers.filter(){
$0.yahooName != ""
}
func parseJSON(completed: @escaping () - ()) {
let url = URL(string: "")
let decoder = JSONDecoder()
let appDelegate = UIApplication.shared.delegate as! AppDelegate //- Intentionally using forced casting.
decoder.userInfo[.managedObjectContext] = appDelegate.persistentContainer.viewContext
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
let fillPlayers = try decoder.decode([CurrentPlayers].self, from: data!)
self.cPlayerArr = fillPlayers.filter(){
$0.yahooName != ""
}
let json = try JSONSerialization.jsonObject(with: data!, options: [])
print(json)
DispatchQueue.main.async {
completed()
}
} catch {
print("JSON Error: ", error)
}
}
}.resume()
}
extension CurrentPlayers {
@nonobjc public class func fetchRequest() - NSFetchRequestCurrentPlayers {
return NSFetchRequestCurrentPlayers(entityName: "CurrentPlayers")
}
@NSManaged public var photoUrl: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
@NSManaged public var position: String?
@NSManaged public var team: String?
@NSManaged public var yahooName: String?
@NSManaged public var status: String?
@NSManaged public var jerseyNumber: Int64
}
extension CurrentPlayers : Identifiable {
}
Post
Replies
Boosts
Views
Activity
I'm trying to find a way to remove instances of empty names in my array. I was trying to find the indices to remove them but it does not print. I could use a little help.
func filterArr() {
for (index, player) in cPlayerArr.enumerated() {
if (player.yahooName == "") {
print("Found \(player) at \(index)")
}
}
}
extension CurrentPlayers {
@nonobjc public class func fetchRequest() - NSFetchRequestCurrentPlayers {
return NSFetchRequestCurrentPlayers(entityName: "CurrentPlayers")
}
@NSManaged public var photoUrl: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
@NSManaged public var position: String?
@NSManaged public var team: String?
@NSManaged public var yahooName: String?
@NSManaged public var status: String?
@NSManaged public var jerseyNumber: Int64
}
extension CurrentPlayers : Identifiable {
}
I downloaded the pod "youtube-ios-player-helper", "~ 1.0.3" and minutes after importing it I get the error No such module 'youtube_ios_player_helper'. Did I install the pod correctly?
podfile
target 'LearnHockey' do
use_frameworks!
pod "youtube-ios-player-helper", "~ 1.0.3"
end
So I am using core data and I have to make my class Decodable to use the decoder for my get request. The errors I get are 'self' used in property access before 'super.init' call in my CoreDataClass each of my decode values has the error.
CoreDataProperties
extension CurrentPlayers {
@nonobjc public class func fetchRequest() - NSFetchRequestCurrentPlayers {
return NSFetchRequestCurrentPlayers(entityName: "CurrentPlayers")
}
@NSManaged public var photoUrl: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
@NSManaged public var position: String?
@NSManaged public var team: String?
@NSManaged public var yahooName: String?
@NSManaged public var status: String?
@NSManaged public var jerseyNumber: Int64
}
extension CurrentPlayers : Identifiable {
}
CoreDataClass
@objc(CurrentPlayers)
public class CurrentPlayers: NSManagedObject, Decodable {
enum CodingKeys: String, CodingKey {
case photoUrl = "PhotoUrl"
case firstName = "FirstName"
case lastName = "LastName"
case position = "Position"
case team = "Team"
case yahooName = "YahooName"
case status = "Status"
case jerseyNumber = "JerseyNumber"
}
required public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
photoUrl = try values.decode(String.self, forKey: CodingKeys.photoUrl)
firstName = try values.decode(String.self, forKey: CodingKeys.firstName)
lastName = try values.decode(String.self, forKey: CodingKeys.lastName)
position = try values.decode(String.self, forKey: CodingKeys.position)
team = try values.decode(String.self, forKey: CodingKeys.team)
yahooName = try values.decode(String.self, forKey: CodingKeys.yahooName)
status = try values.decode(String.self, forKey: CodingKeys.status)
jerseyNumber = Int64(try values.decode(Int.self, forKey: CodingKeys.jerseyNumber))
}
}
I'm trying to transfer a tableview cell from my hockeydetailVC to my favouritesVC using a button and I can't transfer it due to some issue.
My hockeydetailVC
protocol addThis {
func addFav(data: CurrentPlayers)
}
I'm trying to transfer my item object
var item: CurrentPlayers?	
var delegate: addThis? = nil	
func sendData() {
if delegate != nil {
let data = item!
delegate?.addFav(data: data)
print("This is the data being transferred: \(data)")
}
}
@IBAction func addToFav(_ sender: Any) {
sendData()
print("Favourite button Pressed")
}
My favouritesVC
var currentFav: CurrentPlayers?
func addFav(data: CurrentPlayers) {
currentFav = data
}
I have a segue for my button so I have this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "r" {
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
hockeyDetailVC.delegate = self
}
}
If the transfer works then my sendData() method up top would print a statement but my addToFav only prints "favourite button pressed".
I am wondering if there is a way to get UIScrollview to scroll horizontally and vertically in storyboard instead of of just one direction?
I'm trying to add an image to my project and I want it to take up most of the phone's screen but my issue is the resolution is very low and the image is fuzzy, the image is originally small but I am wondering if there are any tricks you can do with swift to make the resolution better at larger sizes. I tried a rounded image but that did not help me.
So I have a searchBar that works when you type in part of the name of a player or players and filters them but when you delete a character or more in the searchbar it does not show anything, I need help trying to add that as a condition in my func.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
parseJson()
} else if searchText != "" {
cPlayerArr = cPlayerArr.filter({ (player) -> Bool in return
player.yahooName.lowercased().contains(searchText.lowercased())
})
}
collections.reloadData()
}
Is there a way to have 2 rows from a CollectionView translate to 2 separate rows of a tableview? (I need both rows to contain separate information)
I have animations on my 2 labels and I can't seem to click on them, I am not sure where the problem lies.
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureR = UITapGestureRecognizer(target: self, action: #selector(self.tapRev))
revLabel.addGestureRecognizer(tapGestureR)
revLabel.isUserInteractionEnabled = true
let tapGestureP = UITapGestureRecognizer(target: self, action: #selector(self.tapPro))
proLabel.addGestureRecognizer(tapGestureP)
proLabel.isUserInteractionEnabled = true
buttonNext.layer.cornerRadius = 10
animatePro()
animateRev()
}
func animatePro() {
UIView.animate(withDuration: 2, delay: 0, options: [.autoreverse], animations: {
UIView.setAnimationRepeatCount(100)
self.proLabel.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}){ (finished) in
self.proLabel
.transform = CGAffineTransform.identity
}
}
func animateRev() {
UIView.animate(withDuration: 2, delay: 0, options: .repeat, animations: {
self.revLabel.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}){ (finished) in
UIView.animate(withDuration: 2, delay: 0, options: .repeat, animations: {
self.revLabel.transform = CGAffineTransform.identity
})
}
}
@objc func tapRev() {
print("gesture rev fired")
self.labelGC.text = "Ultimately, customers are our sources of \nrevenue , and financial stability."
revLabel.alpha = 0
proLabel.alpha = 0
}
@objc func tapPro() {
print("gesture pro fired")
self.labelGC.text = "Ultimately, customers are our sources of \nprofit , and financial stability."
proLabel.alpha = 0
revLabel.alpha = 0
}
I have an issue trying to transfer a number (numText) in a textField. When I type a number it prints 0.
func sendTypedNumToParent() {
if anotherNum != nil {
let data = numText
anotherNum?.sendTyped(data: data)
print("this is the typed number being transferred \(data)")
}
}
I know I have the number when I enter it because I can print it in my return function. What is the issue here?
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
let text = textField.text
let numText = originalNum
if revNumber.text == text {
revSlider.value = Float(numText) / 6.75675675676
revHeight.constant = CGFloat(revSlider.value)
if numText < 316 {
revLabel.text = ""
} else {
revLabel.text = "Revenue"
}
}
		print("I typed this \(numText)")
sendTypedNumToParent()
let name = Notification.Name(rawValue: typedCal)
NotificationCenter.default.post(name: name, object: nil)
return true
}
I'm trying to understand why my if statement does not work.
the numText should change based on it but it does not.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
let text = textField.text
var numText = originalNum
//when you enter a value it will affect, the slider and squareOne width value will change and squareTwo width will change
if widthPer.text == text {
if numText >= 10 || numText <= 90 {
print(numText)
squareIntW = Int(numText)
squareOneWidth.constant = CGFloat(numText) * 2
slider.value = Float(numText)
squareTwoWidth.constant = CGFloat(100 - numText) * 2
squareIntWTwo = Int(100 - numText)
calPercentage()
calPercentageTwo()
} else if numText > 90 { //should not be over 90
numText = 90
print(numText)
squareIntW = Int(numText)
squareOneWidth.constant = CGFloat(numText) * 2
slider.value = Float(numText)
squareTwoWidth.constant = CGFloat(100 - numText) * 2
squareIntWTwo = Int(100 - numText)
calPercentage()
calPercentageTwo()
} else if numText < 10 { //should not be under 10
numText = 10
print(numText)
squareIntW = Int(numText)
squareOneWidth.constant = CGFloat(numText) * 2
slider.value = Float(numText)
squareTwoWidth.constant = CGFloat(100 - numText) * 2
squareIntWTwo = Int(100 - numText)
calPercentage()
calPercentageTwo()
}
}
//when you enter a value it will affect, the sliderHeight and squareOne height will change and squareTwo height will change
if heightPer.text == text {
if numText >= 10 || numText <= 90 {
squareIntH = Int(numText)
squareOneHeight.constant = CGFloat(numText) * 2
sliderHeight.value = Float(numText)
squareTwoHeight.constant = CGFloat(100 - numText) * 2
squareIntHTwo = Int(100 - numText)
calPercentage()
calPercentageTwo()
} else if numText > 90 { //should not be over 90
numText = 90
squareIntH = Int(numText)
squareOneHeight.constant = CGFloat(numText) * 2
sliderHeight.value = Float(numText)
squareTwoHeight.constant = CGFloat(100 - numText) * 2
squareIntHTwo = Int(100 - numText)
calPercentage()
calPercentageTwo()
} else if numText < 10 { //should be under 10
numText = 10
squareIntH = Int(numText)
squareOneHeight.constant = CGFloat(numText) * 2
sliderHeight.value = Float(numText)
squareTwoHeight.constant = CGFloat(100 - numText) * 2
squareIntHTwo = Int(100 - numText)
calPercentage()
calPercentageTwo()
}
}
return true
}
is there a way to type into a textField while having the other textFields disabled but then after you type into that textField having those other textFields you disabled enabled again?
I know how to disable them with .isUserInteractionEnabled but I don't know how to enable them again with func textFieldShouldReturn.
Is there a way for the app not to crash if the user taps on 2 or more textFields without filling them in with numbers?
I'm trying to make my sliders at 0 make no shape but the issue is when any slider hits 0 it makes the shape go to its maximum size. What should I do?
func goToZero() {
if otSlider.value == 0 {
otWidth.constant = 0
print(otWidth.constant)
} else if efSlider.value == 0 {
efWidth.constant = 0
} else if ifSlider.value == 0 {
ifWidth.constant = 0
}
}