If both guns and swords existed in a country, what would be a reason for people to mainly use swords?

Well, it depends on what kinds of guns you’re asking about. There’s the historical reason. While powerful, black power muskets and pistols (and the other variants from the 1700s-1800s) were slow to load and you only had one shot each. This made the gun useful as a battlefield weapon but impractical for use as a primary weapon as the slow loading times at close range meant you couldn’t reload in the heat of battle. In this scenario, the sword, the bow, and the crossbow remained preferable. But, this scenario requires guns that are in the early stages of development. If you’re asking: “why would someone choose to carry a rapier over a Colt 1911”, my answer is “Uh…? Can’t we have both?”.

Japan was one of the first countries to develop guns. However, they outlawed the gun because it threatened (the samurai) their social structure and disrupted their way of life. It’s important to note the Emperor’s decree did not stop other countries from developing guns and when ships from Europe and, eventually, America arrived in their harbor, they faced a distinct technological disadvantage.

Limited access to ammunition and the inability to replace it is one reason. Dystopian settings like Jack McDevitt’s Eternity Road, where the society is still using past technology but have lost the means to replicate it and have fallen back on older types of combat and weaponry they can fabricate.

The characters are facing monsters (alien, supernatural) that a gun cannot reliably put down is another. Warhammer 40k Marines and Imperial Guard often go into battle wielding both a bolt pistol and a chainsword because the bolt pistol cannot reliably put the monster down.

I hope that answers you’re question.