Post

Replies

Boosts

Views

Activity

Reference to member 'delegate' cannot be resolved without a contextual type
Im not sure why, but my tableView.delegate & tableView.data are returning errors "Reference to member 'delegate' cannot be resolved without a contextual type" "Reference to member 'dataSource' cannot be resolved without a contextual type" I've done everything around my code to see why I am getting these errors but I do not understand. My code is below import UIKit @MainActor class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var TableView: UITableView! public var meals = [Meal]() override func viewDidLoad() { super.viewDidLoad() fetchJSON { print("APICompleted") } tableView.delegate = self //error// tableView.dataSource = self //error// } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return meals.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .subtitle , reuseIdentifier: nil) cell.textLabel?.text = meals[indexPath.row].strMeal.capitalized return cell } func fetchJSON(completed: @escaping () -> ()) { let jsonUrlString = "https://www.themealdb.com/api/json/v1/1/filter.php?c=Beef" guard let url = URL(string: jsonUrlString) else { print("invalid url string lel") return } print("Select a meal to make today!") URLSession.shared.dataTask(with: url) { data, response, error in guard let data = data else { return } do { let decoder = JSONDecoder() let mealDetailResponse = try decoder.decode(MealDetailResponse.self, from: data) for meal in mealDetailResponse.meals { print("The Meal is \(meal)") DispatchQueue.main.async { completed() } } }catch { print(error) } }.resume() } }
3
0
4.4k
Nov ’21
request Record Permission in AVFoundation
I am trying to implement a request to use a microphone and i've created a function that records the audio based on the audio status which contains cases of playing, stopped, and recording. When I have updated the method for checking if a user has granted access and to record audio after granting in a if else statement, the compiler is giving me an error "Instance member 'record' cannot be used on type 'AudioBox'; did you mean to use a value of this type instead?" below is the code i've made for the class AudioBox, the enum AudioStatus, and the function that i've created to actually check if the permission is granted. if anyone can tell me what i'm doing wrong with example. @MainActor class AudioBox: NSObject, ObservableObject { @Published var status = AudioStatus.stopped var audioRecorder: AVAudioRecorder? var audioPlayer: AVPlayer? // MARK: Saving audio var urlForVocals: URL { let fileManger = FileManager.default let tempDirectory = fileManger.temporaryDirectory // might need to set this to being a stored dirrectory inisde of the app that saves user's vocals in MP3 format permentally until the app is deleted or when the user deletes the recording/ file let filePath = "TempVocalRecording.caf" return tempDirectory.appendingPathComponent(filePath) } // MARK: setting up an audio recorder func setupRecorder() { let recordingSettings: [String: Any] = [ AVFormatIDKey: Int(kAudioFormatLinearPCM), AVSampleRateKey: 44100.0, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ] do { audioRecorder = try AVAudioRecorder(url: urlForVocals, settings: recordingSettings) audioRecorder?.delegate = self } catch { print("There has been an error creating Audio Recorder") } } // MARK: Recording the audio func record(){ audioRecorder?.record() status = .recording } func stopRecording() { audioRecorder?.stop() status = .stopped } // MARK: Playing the aduio file @MainActor func playSound() { guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return } do { try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default) try AVAudioSession.sharedInstance().setActive(true) let player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue) player.play() } catch let error { print(error.localizedDescription) } } } //MARK: Setting audio delegates extension AudioBox: AVAudioRecorderDelegate { func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) { status = .stopped } } import Foundation var appHasMicAccess = true enum AudioStatus: Int, CustomStringConvertible { case stopped, playing, recording var audioName: String { let audioNames = ["Audio:Stopped", "Audio:Playing", "Audio:Recording"] return audioNames[rawValue] } var description: String { return audioName } } class AudioRecorderViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var audioBox = AudioBox() appHasMicAccess = false } private func requestMicrophoneAccess() { let session = AVAudioSession.sharedInstance() session.requestRecordPermission { granted in appHasMicAccess = granted if granted { AudioBox.record() } else { print("Please Enable Microphone Access in Settings > Privacy > Microphone") } } } }
0
0
694
Dec ’21