Recently, the rapid proliferation of Internet of Things (IoT) technology has led to the development of smart cities, which utilize IoT for various applications, such as traffic monitoring, smart farming, connected vehicles, and environmental data collection. However, one of the most significant challenges faced by smart cities is the ever-present cyber threat to sensitive data. Therefore, a novel IoT-based smart model based on the Fuzzy C-Mean (FCM) and the Sperm Whale Algorithm (SWA), namely, FCM-SWA, was proposed to identify and mitigate cyber-attacks and malicious events within smart cities. First, a recent SWA optimization approach is used to improve FCM's performance and provide effective defenses against various forms of smart city threats. Next, an adaptive threshold strategy is introduced to enhance SWA's global search capabilities and prevent them from converging to local optima. Finally, an efficient scaling approach is proposed as an alternative to traditional normalization methods. The performance of the proposed model is evaluated on three public datasets: NSL-KDD, the Aegean WiFi intrusion dataset (AWID), and BoT-IoT. The accuracy of the proposed FCM-SWA model for the NSL-KDD, AWID, and BoT-IoT datasets is 98.82%, 96.34%, and 97.62%, respectively. Experimental results indicate that the proposed model outperforms related and state-of-the-art techniques in terms of accuracy, detection rate, precision rate, and F1-scores.